UITableView 的增删改 自定义UITableViewCell

1、UITableView的增删改

//设置编辑模式

 

[self.tableView setEditing:YES animated:YES];

//可以不写

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

 

进行对UITabelView编辑时,先对数据源进行操作,后对数组进行操作

(1)删除、插入

//确定编辑的选项

//UITableViewCellEditingStyle

UITableViewCellEditingStyleNone  不编辑

UITableViewCellEditingStyleDelete 删除

 

UITableViewCellEditingStyleInsert 插入

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 

//UITableViewRowAnimation

UITableViewRowAnimationFade,   //淡入淡出

UITableViewRowAnimationRight,  //从右滑入         // slide in from right (or out to right)

UITableViewRowAnimationLeft,   //从左滑入

UITableViewRowAnimationTop,     //从上滑入

UITableViewRowAnimationBottom,  //从下滑入

UITableViewRowAnimationNone,            // available in iOS 3.0

UITableViewRowAnimationMiddle,          // available in iOS 3.2.  attempts to keep cell centered in the space it will/did occupy

UITableViewRowAnimationAutomatic = 100  // available in iOS 5.0.  chooses an appropriate animation style for you

EG:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        [self.dataList removeObjectAtIndex:indexPath.row];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        //刷新表格 无动画显示

        [tableView reloadData];

//插入操作

    }else if (editingStyle == UITableViewCellEditingStyleInsert) {

        [self.dataList insertObject:@"baby" atIndex:indexPath.row];

        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

        [tableView reloadData];

    }

 

}

//更改删除文字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {    

    return @"真的要删除吗";

 

}

此方法默认返回UITableViewCellEditingStyleDelete删除操作

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewCellEditingStyleInsert;  

//多选

return UITableViewCellEditingStyleInsert|UITableViewCellEditingStyleDelete;  

 

}

//移动必须实现的方法

//移动1

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    return YES;

}

//移动2

//sourceIndexPath 起始位置

//destinationIndexPath 目标位置

 

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {

    

    NSString *string = self.dataList[sourceIndexPath.row];

    

    [self.dataList removeObjectAtIndex:sourceIndexPath.row];

    

    [self.dataList insertObject:string atIndex:destinationIndexPath.row];  

 

}

2、批量操作

    //得到所有选中的行数

    NSArray * deleteList = [self.tableView indexPathsForSelectedRows];

    

    //创建删除对象的数组

    NSMutableArray * deleteObjcList = [NSMutableArray array];

    

    for (NSInteger i = 0; i < deleteList.count; i ++) {

        

        NSIndexPath * index = deleteList[i];

        

        //根据选中行数得到相应数组

        NSString * string = self.dataList[index.row];

        

        [deleteObjcList addObject:string];

        

    }

    

    //在数据源里删除所有选中的元素

    [self.dataList removeObjectsInArray:deleteObjcList];

    

    //批量删除动画

 

    [self.tableView deleteRowsAtIndexPaths:deleteList withRowAnimation:UITableViewRowAnimationFade];

3、UITableViewController

(1)cell复用    

第一种形式

//iOS 5.0之前使用

     

static NSString * identifier = @"cellID;

   

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];    

if (!cell) {        

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

}

 

第二种形式

1.注册cell

 

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:identifier1];

2、dequeueReusableCellWithIdentifier

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];

4、自定义单元格cell

1、首先建立model类,并根据KVC进行plist中数组遍历

2、建立继承于UITableViewCell的子类, 并重写- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier 方法 进行自定义控件添加到父视图(单元格)上.

 EG:[self.contentView addSubview:self.iconView];

3、并将model类头文件引入,生成对象将model类的属性赋值给自定义控件

4、在UITableView 将自定义的cell类 生成对象,并返回 数据源中的方法

//layoutSubviews(布局子视图)当执行addsubview

 

- (void)layoutSubviews {}添加几次调用几次

5、nib创建cell

//使用nib注册cell

 

[_tableView registerNib:[UINib nibWithNibName:@"SXTMessageCell" bundle:nil] forCellReuseIdentifier:identifier];

1.第一种

//从工程束里载入nib

UINib * nib = [UINib nibWithNibName:@"SXTMessageCell" bundle:nil];

 

//nib实例化对象

cell = [[nib instantiateWithOwner:self options:nil] lastObject];

             

 

//2.第二种            

 

cell = [[[NSBundle mainBundle] loadNibNamed:@"SXTMessageCell" owner:self options:nil] lastObject];

 

// nib初始化之后第一个执行的方法

- (void)awakeFromNib {}

转载于:https://www.cnblogs.com/PSSSCode/p/5272032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值