国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOSUITableView刪除功能

2019-11-14 18:35:01
字體:
來源:轉載
供稿:網友

  UITbableView作為列表展示信息,除了展示的功能,有時還會用到刪除,比如購物車等。刪除功能可以直接使用系統自帶的刪除功能,當橫向輕掃cell時,右側出現紅色的刪除按鈕,點擊刪除當前cell。

 

  使用系統自帶刪除功能的步驟:

1、讓tableView進入編輯狀態,也就是設置它的editing為YES

2、返回編輯模式,也就是實現UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回刪除模式。如果不實現,默認返回的就是刪除模式

3、提交刪除操作,也就是實現tableview:commitEditingStyle:editing StyleForRowAtIndexPath:方法。只要實現此方法,就默認實現了系統橫掃出現刪除按鈕的刪除方法

4、如果想把刪除按鈕改為中文,可以實現tableView:titleForDeleteConfirmationButtonForRowAtIndexPath方法

代碼:

//  ViewController.m//  JRTableView刪除////  Created by jerehedu on 15/6/11.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#import "ViewController.h"#import "Goods.h"@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>{    UITableView *_tableView; //列表        NSMutableArray *_goodsAry; //商品數組        UIButton *_editBtn; //編輯按鈕}@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];        //添加標題    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];    titleLabel.text = @"購物車";    titleLabel.textAlignment = NSTextAlignmentCenter;    titleLabel.backgroundColor = [UIColor redColor];    titleLabel.textColor = [UIColor whiteColor];    [self.view addSubview:titleLabel];        //添加編輯按鈕    _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];    _editBtn.frame = CGRectMake(self.view.frame.size.width-60, 25, 50, 34);    [_editBtn setTitle:@"編輯" forState:UIControlStateNormal];    [_editBtn setTitle:@"完成" forState:UIControlStateSelected];    _editBtn.titleLabel.font = [UIFont systemFontOfSize:15];    _editBtn.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5];    [self.view addSubview:_editBtn];    [_editBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside];        //添加tableview    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)];    _tableView.dataSource = self;    _tableView.delegate = self;    [self.view addSubview:_tableView];        //取數據    NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]];        //把數據存到模型對象中,然后把對象存到數組中    _goodsAry = [NSMutableArray array];    for (int i=0; i<ary.count; i++) {        Goods *good = [Goods goodsWithDic:ary[i]];        [_goodsAry addObject:good];    }}#PRagma mark 數據源  返回有幾行-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return _goodsAry.count;}#pragma mark 每行顯示內容-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *idGood = @"goods";        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idGood];        if (cell==nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:idGood];    }        Goods *good = _goodsAry[indexPath.row];        cell.imageView.image = [UIImage imageNamed:good.icon];    cell.textLabel.text = good.name;    cell.detailTextLabel.text = good.details;    cell.detailTextLabel.numberOfLines = 6;    cell.detailTextLabel.textColor = [UIColor brownColor];    return cell;}#pragma mark 選中行-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    //取消選中狀態    [tableView deselectRowAtIndexPath:indexPath animated:YES];}#pragma mark 設置行高-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    return 110;}#pragma mark 點擊編輯按鈕- (IBAction)clickEditBtn:(UIButton *)sender {        //設置tableview編輯狀態    BOOL flag = !_tableView.editing;    [_tableView setEditing:flag animated:YES];    _editBtn.selected = flag;}#pragma mark 返回編輯模式,默認為刪除模式-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{     //    return UITableViewCellEditingStyleNone;    //    return UITableViewCellEditingStyleInsert;    return UITableViewCellEditingStyleDelete;}#pragma mark 提交編輯操作-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    //只要實現這個方法,就實現了默認滑動刪除!!!!!    if (editingStyle != UITableViewCellEditingStyleDelete)        return;        //刪除數據模型    [_goodsAry removeObjectAtIndex:indexPath.row];        //刷新界面//    [_tableView reloadData];        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];}#pragma mark 刪除按鈕中文-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{    return @"刪除";}@end////  Goods.h//  購物車表格刪除////  Created by jerei on 15-1-7.//  Copyright (c) 2015年 jerei. All rights reserved.//#import <Foundation/Foundation.h>@interface Goods : NSObject@property (nonatomic, copy) NSString *icon;@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *details;-(id)initWithDic:(NSDictionary*)dic;+(id)goodsWithDic:(NSDictionary*)dic;@end#import "Goods.h"@implementation Goods-(id)initWithDic:(NSDictionary *)dic{    if (self = [super init]) {        self.icon = [dic objectForKey:@"icon"];        self.name = [dic objectForKey:@"name"];        self.details = [dic objectForKey:@"details"];    }    return self;}+(id)goodsWithDic:(NSDictionary *)dic{    Goods *good = [[Goods alloc] initWithDic:dic];    return good;}@end

 

  疑問咨詢或技術交流,請加入官方QQ群:JRedu技術交流 (452379712)

 

作者:杰瑞教育
出處:http://www.survivalescaperooms.com/jerehedu/ 
本文版權歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。
 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹溪县| 台中县| 韶关市| 军事| 衡水市| 婺源县| 来安县| 南木林县| 开江县| 德昌县| 利辛县| 万源市| 通江县| 镇远县| 铜梁县| 仪征市| 开封县| 资溪县| 若尔盖县| 邛崃市| 休宁县| 铜川市| 天长市| 射洪县| 长春市| 时尚| 理塘县| 四子王旗| 神木县| 潜山县| 淅川县| 榕江县| 清水县| 公主岭市| 格尔木市| 横峰县| 昭苏县| 花莲市| 云阳县| 保定市| 泸定县|