我的iOS学习历程 - UITableView的编辑

今天学习的UITableView的编辑,是很重要的一个项目,例如通讯录的实现等都是通过这个来实现的

一般编辑的步骤是:

在编辑之前先设置一下数据源:
便于使用

//  初始化数据
- (void)setUpData {
    NSArray *array = [NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"添加", nil];
    self.firstArray = [NSMutableArray arrayWithArray:array];
    self.secondArray = [NSMutableArray arrayWithArray:array];
}

编辑步骤:
1.开启编辑状态
2.允许哪个分区的哪行是可以编辑的(默认都能编辑)
3.指定可以编辑的样式(删除 or 添加)
4.完成编辑
完成编辑步骤:
(1)操作数据源数组(添加 or 删除)
(2)刷新UI界面
接下来就按照步骤来完成编辑
1.先添加一个编辑按钮,可以点击打开编辑步骤

- (void)addBarButtonItem {
    UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:(UIBarButtonItemStylePlain) target:self action:@selector(barButtonClick:)];
    self.navigationItem.rightBarButtonItem = buttonItem;
    [buttonItem release];
}

2.允许编辑(默认是YES就是所有行可以被编辑)

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    //  可以利用indexPath设置哪些行可以编辑
//    if (indexPath.section == 0 && indexPath.row == 0) {
//        return NO;
//    }
    return YES;
}

3.指定编辑的样式(可以指定编辑的样式,例如删除添加等)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = @"添加";
    //  返回编辑样式
        if([self.firstArray[indexPath.row] isEqualToString:str]) {
            return UITableViewCellEditingStyleInsert;
        }

4.完成编辑(必须有这个方法才能显示出样式)
我们设定有两个分区,两个分区由不同的数组保存数据
切记:进行删除和添加操作时,必要要先对数据源进行操作,然后进行数据的刷新

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //  删除
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //  分区1
        if (indexPath.section == 0) {
            [self.firstArray removeObjectAtIndex:indexPath.row];

//            //  刷新页面
//            //  整体刷新(UITableView) 重新走一遍数据源代理方法达到刷新页面的效果
//            [tableView reloadData];

              //  删除的刷新方法
              //  该方法可以进行多行删除 数组中填所有已经删除的删除的 索引
              [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationMiddle)];
        }else {
            //  分区2
            [self.secondArray removeObjectAtIndex:indexPath.row];
            [tableView reloadData];
            //[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:(UITableViewRowAnimationMiddle)];
        }
        //  添加
    }else if (editingStyle == UITableViewCellEditingStyleInsert) {
        //  分区1
        if (indexPath.section == 0) {
            [self.firstArray insertObject:@"123" atIndex:indexPath.row];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:(UITableViewRowAnimationLeft)];
            [tableView reloadData];
        }else {
            //  分区2
            [self.secondArray insertObject:@"321" atIndex:indexPath.row];
            [tableView reloadData];

        }

    }

}
而编辑中的一种移动又怎么设定呢:

移动步骤:
1.开启编辑状态
2.允许那个分区的那一行 可以编辑
3.完成移动 (1.操作数据源数组 2.刷新界面)
同样按照步骤来:
1.开启编辑状态和以上编辑时开启一样
2.允许那个分区的那一行 可以移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if ([self.firstArray[indexPath.row] isEqualToString:@"添加"]) {
            return NO;
        }
    }if (indexPath.section == 1) {
        if ([self.secondArray[indexPath.row] isEqualToString:@"添加"]) {
            return NO;
    }

}
    return YES;
}

3.完成移动编辑(须有这个步骤才会显示出移动)

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    //  sourceIndexPath来源的索引
    //  destinationIndexPath目的地的索引
    //  移动的分类:1.同分区移动  2.跨分区移动

    //  判断分区移动
    if (sourceIndexPath.section == destinationIndexPath.section) {
        //  同区
        if (sourceIndexPath.section == 0) {
            //  操作firstArray
            //  把来源下的索引 对应的数组元素 保存一下
            NSString *removeStr = self.firstArray[sourceIndexPath.row];
            //  用来源的索引 删除数组中对应的元素
            [self.firstArray removeObjectAtIndex:sourceIndexPath.row];
            //  插入到移动的目的地索引处
            [self.firstArray insertObject:removeStr atIndex:destinationIndexPath.row];
            //  移动刷新方法
            [tableView moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath];
        } else {
            //  操作secondArray
            //  把来源下的索引 对应的数组元素 保存一下
            NSString *removeStr = self.secondArray[sourceIndexPath.row];
            //  用来源的索引 删除数组中对应的元素
            [self.secondArray removeObjectAtIndex:sourceIndexPath.row];
            //  插入到移动的目的地索引处
            [self.secondArray insertObject:removeStr atIndex:destinationIndexPath.row];
        }
    } else {
        //  跨区

    }
}

而我们开发过程是禁止跨区移动的:
限制跨区移动:

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
    //  sourceIndexPath 来源的索引
    //  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、付费专栏及课程。

余额充值