29.table view的编辑模式

来源:互联网 发布:网络小三毛图片 编辑:程序博客网 时间:2024/05/29 08:22
//开启编辑模式:self.tableView.editing = YES;// 用于告诉系统开启的编辑模式是什么模式- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    if (indexPath.row % 2 == 0) {        return UITableViewCellEditingStyleInsert;    }else    {        return UITableViewCellEditingStyleDelete;    }}// 只在在tableview的编辑模式下才有添加// 只要实现该方法, 手指在cell上面滑动的时候就自动实现了删除按钮// commitEditingStyle: 传入提交的编辑操作(删除/添加)// forRowAtIndexPath: 当前正在编辑的行- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    if (UITableViewCellEditingStyleDelete == editingStyle) {        // 1.修改数据        [self.contatcs removeObjectAtIndex:indexPath.row];        // 2.刷新表格        // 该方法用于删除tableview上指定行的cell        // 注意:使用该方法的时候,模型中删除的数据的条数必须和deleteRowsAtIndexPaths方法中删除的条数一致,否则会报错        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];    }else if (UITableViewCellEditingStyleInsert == editingStyle)    {        // 1.修改数据        NJContatc *c = [[NJContatc alloc] init];        c.name = @"xff";        c.phoneNumber = @"123456";        [self.contatcs insertObject:c atIndex:indexPath.row + 1];        NSIndexPath *path = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];        // 注意点:数组中插入的条数必须和tableview界面上插入的cell条一致        // 否则程序会报错        [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];    }}[cell.contentView addSubView: view];在编辑模式的时候才不会出错!
0 0