UITableView分组滑动删除

作者: 芝麻绿豆 | 来源:发表于2016-03-29 21:43 被阅读296次

    首先说一下编辑的代理方法:

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
        return YES;
    }
    -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
           
        }
    }
    

    实现以上代理方法就可以实现删除;
    如果没有分组的话

    // 删除数据源
    [self.addressArr removeObjectAtIndex:indexPath.section];
    // 删除cell
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    

    可是如果你分组的话,这样实现删除就要有bug啦!
    所以需要添加一下代码,实现分组的删除:

    // 更新
        [tableView beginUpdates];
        [self.addressArr removeObjectAtIndex:indexPath.section];
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    // 结束更新
        [tableView endUpdates];
    

    deleteSections:indexSetWithIndex:是用来删除没有cell的分组;本人的需求是一个分组只有一个数据所以不需要判断;如果你是多个数据就要判断一下是不是最后一个cell,如果只有一个cell就要执行这个方法啦;

    相关文章

      网友评论

        本文标题:UITableView分组滑动删除

        本文链接:https://www.haomeiwen.com/subject/sapslttx.html