美文网首页
tableview设置可编辑的cell和可移动位置的cell

tableview设置可编辑的cell和可移动位置的cell

作者: guoguojianshu | 来源:发表于2020-02-03 22:49 被阅读0次
  • 1先设置tableview为可编辑的
[_tableView setEditing:YES];
//屏蔽系统删除模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < 5) {
        return UITableViewCellEditingStyleNone;
    }
    return UITableViewCellEditingStyleDelete;
}
//让table支持移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
//移动某行至某行
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    [_data exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //1.删除该单元格
    [_data removeObjectAtIndex:indexPath.row];
    //2.局部刷新表格
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
//返回yes可以对某行cell可以进行可编辑的,返回no某行cell不可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

相关文章

网友评论

      本文标题:tableview设置可编辑的cell和可移动位置的cell

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