[ios]iOS学习之UITableView(二):进阶篇列表中行的操作

关于列表的创建,上一篇博文已经介绍,本文直接对列表行进行操作,下一篇介绍几种样式的列表:索引,标记和自定义的table

一、选中行

    实现代理方法

1 // 选中行
2 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     NSLog(@"您点击了第%d分区第%d行",indexPath.section, indexPath.row);
5      
6     // 取消选中状态
7     // [tableView deselectRowAtIndexPath:indexPath animated:YES];
8 }
二、删除行

    要对行进行操作,首先要实现代理方法

1 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
2 {
3      
4     return YES;
5 }

    先讲述单独删除一行数据,即左滑出现删除按钮,并删除行的操作,后文会介绍多选批量删除

                              图1 删除行

          

       1. 可重置删除按钮的标题,默认为"delete"

1 // 设置删除按钮标题
2 - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     return @"删除";
5 }

       2. 点击删除后

01 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
02 {
03      
04     // 从数据源中删除
05     [self.dataArray removeObjectAtIndex:indexPath.row];
06      
07     // 从列表中删除
08     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
09     
10 }
三、插入行

     ①这时我将插入行和删除行都以一个按钮动作来触发,点击后tableView进入编辑模式,先上效果图

                   图2 进入添加行模式                                 图3 进入删除行模式

     

      ②在ViewDidLoad中添加代码,其中self.addButton和self.deleteBarButtonItem均在storyBoard中创建,下文中的按钮也是这种情况

1 NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
2      
3 self.navigationItem.leftBarButtonItems = leftBarButtons;//设置导航栏左边按钮为添加和删除按钮

      ③在@interface中声明一个变量

1 UITableViewCellEditingStyle selectEditingStyle;

      ④两个按钮的点击事件

01 // 更新导航栏按钮
02 -(void) updateBarButtons
03 {
04     if (self.tableView.editing==YES) {
05         self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
06     }
07 }
08 // 点击添加按钮
09 - (IBAction)addButtonClicked:(id)sender {
10     selectEditingStyle = UITableViewCellEditingStyleInsert;
11      
12     [self.tableView setEditing:YES animated:YES];
13      
14     [self updateBarButtons];
15      
16 }
17 // 点击删除按钮
18 - (IBAction)deleteButtonClicked:(id)sender {
19      
20      
21     selectEditingStyle = UITableViewCellEditingStyleDelete;
22      
23     [self.tableView setEditing:YES animated:YES];
24      
25     [self updateBarButtons];
26 }

      ⑤实现相应的代理方法

01 // 是否可编辑
02 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
03 {
04      
05     return YES;
06 }
07 // 编辑模式
08 -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
09 {
10     return selectEditingStyle;
11      
12 }
13  
14  
15 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
16 {
17     // 删除模式
18     if (editingStyle==UITableViewCellEditingStyleDelete) {
19          
20         // 从数据源中删除
21         [self.dataArray removeObjectAtIndex:indexPath.row];
22         // 删除行
23         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
24     }
25     // 添加模式
26     else if(editingStyle == UITableViewCellEditingStyleInsert){
27          
28         // 从数据源中添加
29         [self.dataArray insertObject:@"new iPhone" atIndex:indexPath.row];
30          
31         // 添加行
32         [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic ];
33     }
34     
35 }
36 // 点击完成按钮
37 - (IBAction)doneButtonClicked:(id)sender {
38      
39     [self.tableView setEditing:NO animated:YES];
40      
41     [self updateBarButtons];
42 }


四、移动行

      ①效果图

                                图4 移动行

            

     ②在tableView进入编辑模式时,可以对行进行移动操作,通过方法

1 // 是否支持移动
2 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     return YES;
5 }

     ③设置行可移动,并完成移动行方法,改变数据源

1 // 移动行操作
2 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
3 {// 这里其实就是数组中两个变量交换位置的过程
4     id object = [self.dataArray objectAtIndex:fromIndexPath.row];
5      
6     [self.dataArray removeObjectAtIndex:fromIndexPath.row];
7      
8     [self.dataArray insertObject:object atIndex:toIndexPath.row];
9 }
五、批量删除行

     ①即完成可以选择多个行之后批量删除,如图

                    图5 进入批量删除模式                                  图6 选中行准备删除

       

      ②在ViewDidLoad中添加代码

1 self.navigationItem.rightBarButtonItem = self.editBarButtonItem;// 在右导航栏中添加编辑按钮

     ③现在需要达到,点击编辑按钮在右上角出现取消按钮,左上角出现删除按钮。并在选择时,能出现删除行的数量,修改updateBarButtons方法,并添加一个方法来根据条件修改删除按钮的标题

01 // 更新导航栏按钮
02 -(void) updateBarButtons
03 {
04     // 如果是允许多选的状态,即进入批量删除模式
05     if (self.tableView.allowsSelectionDuringEditing == YES) {
06         //更新删除按钮
07         [self updateDeleteButtonTitle];
08         // 导航栏左边按钮设置为空
09         self.navigationItem.leftBarButtonItems = nil;
10         // 将左边按钮设置为'批量删除'按钮
11         self.navigationItem.leftBarButtonItem = self.multiDeleteBarButton;
12         // 导航栏右键设置为'取消'键
13         self.navigationItem.rightBarButtonItem = self.cancelBarButtonItem;
14          
15         return;
16     }
17     if (self.tableView.editing==YES) {// 如果是编辑状态,且不属于批量删除状态
18         // 导航栏右键设置为'取消'键
19         self.navigationItem.rightBarButtonItem = self.doneBarButtonItem;
20     }
21     else {// 如果不是编辑状态,将导航栏设置为初始状态的样式,即左栏为'添加','删除'按钮,右栏为'编辑'按钮
22         NSArray *leftBarButtons = [NSArray arrayWithObjects:self.addButton,self.deleteBarButtonItem, nil];
23         self.navigationItem.leftBarButtonItems = leftBarButtons;
24          
25         self.navigationItem.rightBarButtonItem = self.editBarButtonItem;
26     }
27 }


01 // 更新删除按钮的标题
02 -(void)updateDeleteButtonTitle
03 {
04     NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];//得到选中行
05      
06     BOOL allItemsAreSelected = selectedRows.count == self.dataArray.count;// 是否全选
07      
08     BOOL noItemsAreSelected = selectedRows.count == 0;// 选中行数是否为零
09      
10     if (allItemsAreSelected || noItemsAreSelected)
11     {// 如果是全选或者未选,则删除键为删除全部
12         self.multiDeleteBarButton.title = @"删除全部";
13     }
14     else
15     {// 否则 删除键为删除(选中行数量)
16         self.multiDeleteBarButton.title = [NSString stringWithFormat:@"删除 (%d)", selectedRows.count];
17     }
18  
19 }

④在

1 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
2  
3 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath

两个方法中调用updateDeleteButtonTitle方法

⑤点击编辑按钮时

1 // 编辑按钮
2 - (IBAction)editButtonClicked:(id)sender { 
3     self.tableView.allowsMultipleSelectionDuringEditing = YES;// 进入可多选删除状态
4      
5     [self.tableView setEditing:YES animated:YES];// 将table设置为可编辑
6      
7     [self updateBarButtons];  //更改导航栏的导航按钮
8 }

    ⑥点击删除多个按钮时

01 - (IBAction)multiDeleteClicked:(id)sender {
02     // 选中的行
03     NSArray *selectedRows = [self.tableView indexPathsForSelectedRows];
04      
05     // 是否删除特定的行
06     BOOL deleteSpecificRows = selectedRows.count > 0;
07     // 删除特定的行
08     if (deleteSpecificRows)
09     {
10         // 将所选的行的索引值放在一个集合中进行批量删除
11         NSMutableIndexSet *indicesOfItemsToDelete = [NSMutableIndexSet new];
12          
13         for (NSIndexPath *selectionIndex in selectedRows)
14         {
15             [indicesOfItemsToDelete addIndex:selectionIndex.row];
16         }
17         // 从数据源中删除所选行对应的值
18         [self.dataArray removeObjectsAtIndexes:indicesOfItemsToDelete];
19          
20         //删除所选的行
21         [self.tableView deleteRowsAtIndexPaths:selectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
22     }
23     else
24     {
25         // 删除全部
26         [self.dataArray removeAllObjects];
27          
28         [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
29     }
30     // 删除完成,退出编辑状态,并退出多选状态,同时更新导航栏的按钮
31     [self.tableView setEditing:NO animated:YES];
32      
33     self.tableView.allowsMultipleSelectionDuringEditing = NO;
34      
35     [self updateBarButtons];
36 }
代码下载

http://www.oschina.net/action/code/download?code=33584&id=48435

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值