zoukankan      html  css  js  c++  java
  • UITableView编辑

    tableview 的编辑可以分为四个步骤:

    1、让tableView处于可编辑状态

    2、指定哪些行可以被编辑

    3、指定编辑样式(delete还是insert)

    4、完成编辑(1、修改数据源,2、修改UI界面)

     

    1、让tableView处于可编辑状态

    (1)self.rv.tabelView.editing == YES

    BOOL类型的editing,设置为YES,即为可编辑状态,为NO则为不可编辑状态。

    (2)setEditing:!self.rv.tabelView.editing animated:YES

    第一个参数:BOOL类型的,为YES则可编辑,为NO不可编辑

    第二个参数:BOOL类型的,为YES则添加动画,为NO不添加动画

    RootViewController.m

     

    #pragma mark 响应方法

     

    - (void)leftAction:(UIBarButtonItem *)sender{

        NSLog(@"添加..");

        // 记录添加样式

        _editingStyle = UITableViewCellEditingStyleInsert;

        // 1.让tableView处于编辑状态

        // 加动画

        [self.rv.tabelView setEditing:!self.rv.tabelView.editing animated:YES];

    }

    - (void)rightAction:(UIBarButtonItem *)sender{

        NSLog(@"删除..");

        // 记录删除样式

        _editingStyle = UITableViewCellEditingStyleDelete;

       

    //    if (self.rv.tabelView.editing == YES) {

    //        [self.rv.tabelView setEditing:NO animated:YES];

    //    }else{

    //        [self.rv.tabelView setEditing:YES animated:YES];

    //    }

        // 让tableView处于编辑状态 和 退出编辑状态

        [self.rv.tabelView setEditing:!self.rv.tabelView.editing animated:YES];

    }

    解释:

    navigation的响应事件(导航条左边为添加,右边为删除):

    1、setEditing:!self.rv.tabelView.editing,这里直接取!(取反),是为了确定tableView是否处于编辑状态,如果是编辑状态,则设置成不可编辑状态,如果是飞编辑状态,则设置成可编辑状态。

    2、_editingStyle = UITableViewCellEditingStyleInsert; 是用来获取编辑样式的,其实应该说是记录样式,如果记录的样式是insert,则在后面提交的时候,往数据源添加数据,如果记录的样式是delete,那么再后面提交的时候,往把数据源的要删除的数据删除。

     

    2、指定哪些行可以被编辑

    // 2.指定哪些行可以被编辑

    // 询问每一行处于什么状态(走行数那么多次)

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{   

    //    if (indexPath.row == 0) {

    //        return YES;

    //    }else{

     

    //        return NO;

    //    }

        return YES;

     

    }

     

    在创建cell 的时候,会每创建一个cell都会走一遍这个方法,目的是为了询问该行是否可编辑(与是否处于编辑状态不一样)。这里直接return YES的意思是告诉系统,我的每行都可以编辑。

    指定编辑样式(delete还是insert)

    #pragma mark delegate

     

    // 3.指定编辑样式

    - (UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

       

        return _editingStyle;

    }

    解释:

    这个方法是delegate里的方法;

    这个方法里,直接return _editingStyle,是返回我们上面记录的编辑样式(delete还是insert)

    完成编辑(1、修改数据源,2、修改UI界面)

    // 4.完成编辑

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

        NSLog(@"编辑完成..");

        // 判断为删除样式

        if (editingStyle == UITableViewCellEditingStyleDelete) {

            // 1.修改数据源

            [self.dataArr[indexPath.section] removeObjectAtIndex:indexPath.row];

            //    [self.dataArr[indexPath.section] removeObjectAtIndex:indexPath.row+1];

            // 2.修改UI界面

            //    [tableView reloadData];

           

            //    NSIndexPath *ind = [NSIndexPath

    indexPathForRow:indexPath.row +1 inSection:indexPath.section];

            // @[indexPath]告诉你是哪一行(数组)

            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];

        }else if(editingStyle == UITableViewCellEditingStyleInsert){// 判断为添加

            // 执行添加

            // 1. 修改数据源

    //        [self.dataArr[indexPath.section] addObject:@"我是新来的.."];

            [self.dataArr[indexPath.section] insertObject:@"我是新来的哦" atIndex:indexPath.row + 1];

            // 2.修改UI

    //        [tableView reloadData];

            // +1是为了控制再后一行出现

            NSIndexPath *ind = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

            [tableView insertRowsAtIndexPaths:@[ind] withRowAnimation:(UITableViewRowAnimationLeft)];

        }

    }

    解释:

    1、根据editingStyle判断返回来的编辑样式是insert还是delete,分别对数据源进行不同操作。

    2、判断为insert编辑样式,修改数据源,[self.dataArr[indexPath.section] insertObject:@"我是新来的哦" atIndex:indexPath.row + 1],在数组中改行的后面插入一个对象。indexPath.section作为数组的元素个数,是因为,前面定义的数据源的结构是一个可变数组里边嵌套的两个可变数组,这样self.dataArr[indexPath.section]就是取到外层大数组的的元素下标,这里的section的值就是数组的元素个数。indexPath.row + 1是大数组下标下,取得的小数组,小数组里元素的下标。这样就能把我们想要插入的东西插入到我们想要插入的地方。

    3、NSIndexPath *ind = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];重新给indexPath设置新值,为了后面的多位置添加。

    4、修改数据源完成后修改UI界面[tableView insertRowsAtIndexPaths:@[ind] withRowAnimation:(UITableViewRowAnimationLeft)],ind数组是重新设置的indexPath的值,里边有多个section和 row。也就可以添加多个位置的信息。

    5、删除和添加一样,但是要把添加变成删除。[self.dataArr[indexPath.section] removeObjectAtIndex:indexPath.row]。

  • 相关阅读:
    网络编程中 TCP 半开连接和TIME_WAIT 学习
    redis中的小秘密和持久化小细节
    排序
    Es官方文档整理-3.Doc Values和FieldData
    Es官方文档整理-2.分片内部原理
    Elasticsearch doc_value认识
    路边停靠 贴边停车不蹭轮胎的技巧
    mybatis 连接数据库
    putIfAbsent
    Hive与HBase区别 大墨垂杨
  • 原文地址:https://www.cnblogs.com/Coder-GT/p/4886137.html
Copyright © 2011-2022 走看看