您的位置:首页 > 产品设计 > UI/UE

iOSDay30之UITableView编辑

2016-05-11 16:43 344 查看
1. UITableView编辑

  1> UITableView 编辑流程

  2> UITableView 编辑步骤(四步)

  ① 第一步 : 让 TableView 处于编辑状态(在按钮点击事件方法中) 

1     // 优化写法
2     // 不带动画
3     _rootView.tableView.editing = !_rootView.tableView.editing;
4     // 带动画
5     [_rootView.tableView setEditing:!_rootView.tableView.editing animated:YES];


  ② 协议设定

   第二步 : 确定cell是否处于编辑状态(UITableViewDataSource协议的方法) 

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// 所有的cell都可以进行编辑时,整个方法可以省略
// return YES;

// 只有第一个分区可以被编辑
if (0 == indexPath.section) {
return YES;
}
return NO;
}


   第三步 : 设定cell的编辑样式 (删除 , 添加)

1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     return UITableViewCellEditingStyle枚举中的样式;
4 }


   第四步 : 编辑状态进行添加

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     // 1. 处理数据
4     // 2. 更新UI界面
5 }


 3> 添加(前两步通用)

  第三步:  

1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     return UITableViewCellEditingStyleDelete;
4 }


  第四步:

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3         // 1. 删除数据
4         [_allDataArray[indexPath.section] removeObjectAtIndex:indexPath.row];
5
6         // 2. 更新UI
7         // 单独更新一行(删除)
8         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
9
10         // 全部更新
11 //        [tableView reloadData];
12 }


 4> 删除(前两步通用)

  第三步:

1 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     return UITableViewCellEditingStyleInsert;
4 }


  第四步:

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     // 1. 插入数据到数组中
4         [_allDataArray[indexPath.section] insertObject:@"你是不是傻" atIndex:indexPath.row + 1];
5
6         // 2. 更新UI
7         // 全部更新
8 //        [tableView reloadData];
9
10         // 单独更新一行
11
12         // 创建新一行的NSIndexPath对象
13         NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
14
15         [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
16 }


 5>添加 和 删除 结合   

  第三步 : 设置一个 UITableViewCellEditingStyle 类型的 属性(style) 用于存储 添加 和 删除 的编辑的样式, 在 添加 和 删除 对应的点击事件方法中赋值, 注:按钮的功能样式需要在点击事件最上面实现, 否则会出现bug

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.style;
}


  第四步 :  

1 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3     // 判断编辑样式
4     if (UITableViewCellEditingStyleDelete == editingStyle) {
5         删除操作,详情请见 3> 第四步
6     } else if (UITableViewCellEditingStyleInsert == editingStyle){
7         添加操作,详情请见 4> 第四步
8     }
9 }


 6> 移动

  ① (在 TableView 处于编辑状态下)实现协议: 告诉 tableView 是否能够移动 

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}


  ② 移动

1 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
2 {
3     // 1. 获取需要修改的数据
4     NSString *sourceData = [self.allDataArray[sourceIndexPath.section] objectAtIndex:sourceIndexPath.row];
5
6     // 2. 先将数据从当前的位置移除
7     [self.allDataArray[sourceIndexPath.section] removeObjectAtIndex:sourceIndexPath.row];
8
9     // 3. 将数据插入到对应的位置
10     [self.allDataArray[destinationIndexPath.section] insertObject:sourceData atIndex:destinationIndexPath.row];
11 }


  bug修正--- 防止跨分区移动

1 - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
2 {
3     // sourceIndexPath 为原位置
4     // proposedDestinationIndexPath 为将要移动到的位置
5     if (sourceIndexPath.section == proposedDestinationIndexPath.section) {
6         return proposedDestinationIndexPath;
7     } else {
8         return sourceIndexPath;
9     }
10 }


2. UITableViewController

 1> 概述

  UITableViewController 是继承于 UIViewController 中的一个类,只不过比UIViewController 中多了一个属性 tableView 。 即: UITableViewController 是一个自带 table 的视图控制器。

 2> 注意事项

UITableViewController 继承 UIViewController , 自带一个tableView

self.view 不是 UIView  是 UITableView

datasource 和 delegate 默认都是 self (UITableViewController)

开发中只需要建 UITableViewController 子类

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: