美文网首页
iOS_UI_11_UITableView的编辑

iOS_UI_11_UITableView的编辑

作者: 孤城99 | 来源:发表于2016-12-09 21:47 被阅读0次

    第十一章 UITableView的编辑

    一、UITableView编辑
    1.UITableView编辑步骤
        1.打开编辑状态
           - (void)setEditing:(BOOL)editing animated:(BOOL)animated{
               //一步:要打开tableView的编辑状态,我们首先要获取tableView对象
               UITableView* mTableView = [self.view viewWithTag:1000];
               [mTableView setEditing:editing animated:animated];
               [mTableView reloadData];
           }
        2.协议设定
            1.确定Cell是否处于编辑状态
               - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
            2.设定Cell的编辑样式(删除、添加)
               -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
            3.编辑状态进行提交
               - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
                   if (editingStyle == UITableViewCellEditingStyleDelete) {
                       //说明做的事删除操作
                       NSLog(@"你进行了删除操作");
                       //将数组中对应位置的数据干掉
                       [self.allDataMArray removeObjectAtIndex:indexPath.row];
                       //界面刷新  当数据源发生改变的时候我们需要刷新界面,让新的数据重新显示到界面上
               //        [tableView reloadData];//刷新整个视图
                      //删除对应位置的单元格
                      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
        
        
                    }
                   if (editingStyle == UITableViewCellEditingStyleInsert) {
                   //说明做的是插入操作
                   NSLog(@"你进行了插入操作");
                   //先将数据添加到数组中
                   [self.allDataMArray addObject:@"新增"];
                   //增加单元格
                    [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationTop)];
                    }
               }
        注意:编辑结束后,由于numberOfRowsInSection这个协议只在tableView添加到父视图的时候走一次,而且table上的数据都是由数组提供,因此,需要想将数组中的元素删除,然后让table的协议重新走一遍进行重新赋值
              即:先修改数据源,再刷新table(使用reloadData方法)
    2.UITableView的移动
        1.实现协议:告诉tableView是否能够移动
            -(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;
        2.移动
            - (void)tableView:(UITableView *)tableView  moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
                NSLog(@"移动结束了");
                //将移动的单元格上的数据提出来,保存起来
                NSString* string = self.allDataMArray[sourceIndexPath.row];
                //删除数组中的source位置的数据
                [self.allDataMArray removeObjectAtIndex:sourceIndexPath.row];
                //将数据插入到新的位置
                [self.allDataMArray insertObject:string atIndex:destinationIndexPath.row];
            }
    3.设置某一行的编辑样式
        - (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
           if (indexPath.row == self.allDataMArray.count) {
               //说明是最后一行,让最后一行的样式插入
               return UITableViewCellEditingStyleInsert;
            }
            //其余行未删除样式
            return UITableViewCellEditingStyleDelete;
        }
    4.设置某一行能否移动  返回值为YES:该cell可以移动
        indexPath:能否移动的行的位置
         - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
         //设置添加联系人哪行不能移动
          if (indexPath.row == self.allDataMArray.count) {
           return NO;
          }
           return YES;
        }
    5.移动过程中会执行的代理方法
        sourceIndexPath:cell原位置
        proposedDestinationIndexPath:想要(可能)移动到的位置
        - (NSIndexPath*)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath{
            //如果cell将要移动到的位置时添加联系人,那么我们就让他返回原位置
            if (proposedDestinationIndexPath.row == self.allDataMArray.count) {
               return sourceIndexPath;
            }
               return proposedDestinationIndexPath;
         }
    6.进行编辑操作的按钮样式
        - (NSArray<UITableViewRowAction *>*)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
          UITableViewRowAction* action_1 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDefault) title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                [self.allDataMArray removeObjectAtIndex:indexPath.row];
                //界面刷新  当数据源发生改变的时候我们需要刷新界面,让新的数据重新显示到界面上
                //        [tableView reloadData];//刷新整个视图
                //删除对应位置的单元格
                [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationRight)];
                NSLog(@"点击删除按钮");
            }];
            action_1.backgroundColor = [UIColor greenColor];
          UITableViewRowAction* action_2 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleDestructive) title:@"更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                NSLog(@"点击更多按钮");
            }];
          UITableViewRowAction* action_3 = [UITableViewRowAction rowActionWithStyle:(UITableViewRowActionStyleNormal) title:@"取消操作" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
                NSLog(@"点击取消按钮");
          }];
          return @[action_1,action_2,action_3];
         }
    
    二、UITableViewController
    1.UITableViewController继承自UIViewController,自带一个tableView
    2.self.view不是UIView而是UITableView
    3.datasource和delegate默认都是self(UITableViewController)
    4.开发之需要建立UITableViewController子类

    相关文章

      网友评论

          本文标题:iOS_UI_11_UITableView的编辑

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