TableView实现基本的edit insert delete reorder功能

进入编辑TableView编辑模式(右上角的编辑按钮的回调方法)

//edit按钮的回调方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    
    if(editing) {
        self.editButtonItem.title = @"完成";
    }
    else {
        self.editButtonItem.title = @"编辑";
    }
    [self.tableView setEditing:editing animated:animated];
}

设置Row左侧的小图标

row的小图标有三种内置的选择: insert,delete,none。

//设置Row左侧的小图标
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.row % 2 == 0) {
        return UITableViewCellEditingStyleDelete;
    }
    else{
        return UITableViewCellEditingStyleInsert;
    }
}

侧滑button的回调方法

在Controller中必须存在该方法才能出现侧滑效果。侧滑button的回调方法用于响应侧滑出现的button。

当点击Row左侧的内置小图标时,同样也会产生侧滑效果。因此,最终中的侧滑逻辑都是在下面的回调方法中实现的。

//该方法是侧滑出来的按钮的回调方法
//并且在Controller中必须存在该方法才能出现侧滑效果
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if(editingStyle == UITableViewCellEditingStyleDelete) {
        NSLog(@"response to delete button");
        //物理删除
        [self.countries removeObjectAtIndex:indexPath.row];
        
        //界面删除
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
    else if(editingStyle == UITableViewCellEditingStyleInsert) {
        NSLog(@"response to add button");
        //物理添加
        Country *originalCountry = [self.countries objectAtIndex:indexPath.row];
        Country *addCountry = [[Country alloc] initWithName:originalCountry.name];
        [self.countries insertObject:addCountry atIndex:indexPath.row];
        
        //界面添加
        //为新添加行构造一个indexPath,并放入数组中
        NSArray *indexPathArray = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]];
        [self.tableView insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationFade];
    }
}

实现Cell Reorder重排功能

为了实现Cell Reorder功能,必须实现下列两个方法。另外,使用Cell Reorder前必须激活编辑模式(右上角的编辑按钮)

//设置哪些row可以做reorder操作
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//move操作的回调方法
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    
    //物理重排
    [self.countries exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
    
    //重新加载数据
    [self.tableView reloadData];
}

Demo

https://git.oschina.net/iSingular/AddDeleteReorderRows.git

转载于:https://my.oschina.net/isingular/blog/618021

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值