美文网首页
关于tableView的编辑模式

关于tableView的编辑模式

作者: NateLam | 来源:发表于2016-10-20 22:06 被阅读1745次

    苹果已经帮我们做好了大部分UI工作, 我们只需要专注数据处理即可

    设置navigationbarItem的编辑按钮, 不用alloc, 直接

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    

    点击会自动变为完成, 再点击会变回编辑, 无需自己写, 使用self.tableView.editing即可判断是否进入编辑状态

    然后重写setEditing方法, 以下用到的两个数组一个是保存被选中的indexPath, 一个是保存被选中的cell所拥有的数据元素, 每次重新进入编辑时初始化

    #pragma mark - 重写setEditing方法
    - (void)setEditing:(BOOL)editing animated:(BOOL)animated{
            [super setEditing:editing animated:animated];
    
            [_tableView setEditing:!_tableView.editing animated:YES];
    
            if (self.tableView.editing) {
        
                _mArrOfIndex = [NSMutableArray array];
                _mArrOfSource = [NSMutableArray array];
        
                [self showDeleteView];
            }
            else{
                [self hideDeleteView];
        }
    }
    

    以下这句不写之前, 进入编辑后, 全体cell向右缩进露出的的是红色减号, 点击后是像rowaction一样向左缩进然后露出红色删除键;
    而写了这句话之后, 全体cell向右缩进露出的是灰色圆圈, 点击可多选, 打上蓝色对勾

    _tableView.allowsMultipleSelectionDuringEditing = YES;
    

    以下两个方法是点击cell和取消选中的协议方法,

    #pragma mark  点击row
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (tableView.editing) {
        
            NSString *str = _mArrOfData[indexPath.row];
            if (![_mArrOfIndex containsObject:str]) {
                [_mArrOfIndex addObject:str];
            }
          
            [_mArrOfSource addObject:indexPath];
        }
        else{
    
            //取消选中状态
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
          }
    }
    
    #pragma mark  点击已被点击过的row
    - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if (tableView.editing) {
        
            //从数据数组取出指定元素
            NSString *str = _mArrOfData[indexPath.row];
            if ([_mArrOfIndex containsObject:str]) {
                [_mArrOfIndex removeObject:str];
            }
        
        
            [_mArrOfSource removeObject:indexPath];
    //        [_mArrOfIndex removeObject:indexPath];
        }
    }
    

    我在进入编辑时从底部浮上来一个菜单栏, 有删除和取消两个button, 对应的点击事件, 先删数据, 再更新UI, mArrOfIndex保存的是选中的数据们, mArrOfData保存的是选中的indexPath们

      kWeakSelf(self);
          _bottomDeleteView.deleteArticle = ^(UIButton *btn){
        
              if (_mArrOfIndex.count > 0) {
            
                [weakself.mArrOfData removeObjectsInArray:weakself.mArrOfIndex];
                [weakself.tableView deleteRowsAtIndexPaths:weakself.mArrOfSource withRowAnimation:UITableViewRowAnimationLeft];
            }
            
            [weakself hideDeleteView];
            [weakself.tableView setEditing:NO animated:YES];
            weakself.navigationItem.rightBarButtonItem.title = @"编辑";
        };
        _bottomDeleteView.cancelDelete = ^(UIButton *btn){
            
            [weakself hideDeleteView];
            [weakself.tableView setEditing:NO animated:YES];
            weakself.navigationItem.rightBarButtonItem.title = @"编辑";
    
        };
    

    以下这俩方法应该是在单选时才生效

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
        return YES;
    }
    
    
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        }
    }
    

    相关文章

      网友评论

          本文标题:关于tableView的编辑模式

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