UI_UITableView编辑

向左滑动

#pragma mark - 设置 tableView 能否编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

#pragma mark - 左滑动哪种编辑状态
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
#pragma mark - 实现左滑动编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了删除");
    // 实现 cell 编辑,必须先对数据源进行修改
    // 找到分组
    NSMutableArray *groupArray = self.dataDic[self.keysArray[indexPath.section]];

    // 判断该分组里面是否只有最后一个值
    if (groupArray.count < 2) {
        // 删除数据源(分组)
        [self.dataDic removeObjectForKey:self.keysArray[indexPath.section]];
        // 删除 keysArray 里面的 key 值
        [self.keysArray removeObjectAtIndex:indexPath.section];

        // 同时删除 UI_分组
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
        [tableView deleteSections:indexSet withRowAnimation:UITableViewRowAnimationBottom];

    } else {
        // 删除数据源 (cell)
        [groupArray removeObjectAtIndex:indexPath.row];
        // 同时删掉 UI_cell
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
    }
}
#pragma mark - 修改删除字样
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

上下拖动,交换位置

#pragma mark - 指定那些 cell 可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 如:禁止删除第一个
    if (indexPath == 0) {
        return NO;
    }
    return YES;
}
#pragma mark - 实现移动
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
    // 改变数据源
    // 1、拿到小数组
    NSMutableArray *array = self.dataDic[self.keysArray[sourceIndexPath.section]];

    // 2、交换数组里面的数据
    // 拿出原来的数据
    Person *p = array[sourceIndexPath.row];
    Person *aperson = [[Person alloc] init];
    aperson.name = p.name;
    aperson.age = p.age;
    aperson.gender = p.gender;
    aperson.phoneNumber = p.phoneNumber;
    aperson.hobby = p.hobby;
    aperson.picture = p.picture;

    // 删除原来的数据
    [array removeObject:p];

    // 插入到目的位置
    [array insertObject:aperson atIndex:destinationIndexPath.row];

    // 3、改变 UI

    [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
    //    [tableView reloadData];
}
#pragma mark - 限制跨区域移动
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    // 判断分组是否相同
    if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
        return proposedDestinationIndexPath;
    } else {
        return sourceIndexPath;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值