美文网首页iOS开发好文程序员iOS Developer
用系统方法自定义滑动Cell 视图(兼容iOS7)

用系统方法自定义滑动Cell 视图(兼容iOS7)

作者: 洛少城 | 来源:发表于2016-07-21 18:30 被阅读258次

    实现效果

    实现系统原生的删除方法

    • 必须要实现系统原生的两个方法
      1. 方法editingStyleForRowAtIndexPath:为设置表格的样式为删除样式;
      2. 方法titleForDeleteConfirmationButtonForRowAtIndexPath:为编辑时显示的文字,这里讲的是自定义,所以这里的文字可以传空字符串,避免自定义后文字重叠异常显示, 系统默认显示的文字为 "删除";

    注意: 这里的传的空字符串的长度决定你自定义后显示多少个按钮的总宽度。

    #pragma mark - <UITableViewDelegate>
    
    /**
     *  实现表格系统方法的编辑模式为删除模式
     */
    - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
        return UITableViewCellEditingStyleDelete;
    }
    
    /**
     *  设置滑出cell后的文字为空字符串
     */
    - (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
        return @"                         ";
    }
    

    数据源的代理方法实现不需要额外设置什么

    #pragma mark - <UITableViewDataSource>
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *identifie = @"UITableViewCell";
        GJEmployeeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifie];
        if (!cell) {
            cell = [[GJEmployeeCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifie];
        }    
        //选择的cell
        WEAKSELF
        cell.slectedActionBlock = ^(SwipeCellActionType cellActionType, UITableViewCell *cell){
            //防止cell出现选中效果
            NSIndexPath *editIndex = [tableView indexPathForCell:cell];
            [tableView reloadRowsAtIndexPaths:@[editIndex] withRowAnimation:UITableViewRowAnimationNone];
            
            GJEmployeeModel *editEmployee = weakSelf.dataArr[editIndex.row];
            if (cellActionType == EditCellType) {
                //处理编辑按钮事件...
            } else {
                //处理删除按钮事件...
            }
        };
        return cell;
    }
    
    

    自定义Cell(.m)文件的逻辑

    • 关键点:重写cell的 layoutSubviews方法
    • layoutSubviews方法中遍历寻找到滑动完Cell后底部显示的视图, 然后再添加需要自定义的按钮的到底部的视图上;

    注意: 这里要根据当前的手机系统版本来区分底部的视图是属于哪种类型的视图:
    1.在iOS8以下的系统经过亲自测试遍历到底部的视图属于:UITableViewCellDeleteConfirmationView这种类型的视图;
    2.在iOS8以上的系统经过亲自测试遍历到底部的视图属于:_UITableViewCellActionButton这种类型的视图;

    /**
     *  根据ios系统版本自定义滑动删除按钮
     */
    - (void)layoutSubviews
    {
        [super layoutSubviews];
        for (UIView *subview in self.subviews) {
    
            for (UIView *subview2 in subview.subviews) {
                
                //根据ios系统版本判断滑动删除视图View, (关键部分)
                NSString *systemDeleteViewName = nil;
                if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
                    systemDeleteViewName = @"UITableViewCellDeleteConfirmationView";
                    
                } else {
                    systemDeleteViewName = @"_UITableViewCellActionButton";
                }
                
                if ([NSStringFromClass([subview2 class]) isEqualToString:systemDeleteViewName]) {
                    
                    NSLog(@"subview2 属于什么View====%@",subview2);
                    
                    //防止多次添加自定义按钮
                    [subview2.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
                    
                    //subview2的宽度由控制器里面的代理方法(tableView:titleForDeleteConfirmationButtonForRowAtIndexPath:)返回的文字宽度决定
                    
                    [subview2 addSubview:self.editButton];
                    self.editButton.width = subview2.width/2;
                    
                    [subview2 addSubview:self.deleteButton];
                    self.deleteButton.x = CGRectGetMaxX(self.editButton.frame);
                    self.deleteButton.width = self.editButton.width;
                    NSLog(@"subview2====%zd",subview2.subviews.count);
                }
            }
        }
    }
    
    - (UIButton *)editButton
    {
        if (!_editButton) {
            _editButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, self.height)];
            _editButton.backgroundColor = UIColorFromRGB(0xbbbbbb);
            [_editButton setImage:ImageNamed(@"editButton_icon") forState:0];
            _editButton.tag = EditCellType;
            [_editButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        return _editButton;
    }
    
    - (UIButton *)deleteButton
    {
        if (!_deleteButton) {
            _deleteButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 0, 80, self.height)];
            _deleteButton.backgroundColor = UIColorFromRGB(0xfe7270);
            [_deleteButton setImage:ImageNamed(@"deleteButton_icon") forState:0];
            _deleteButton.tag = DeleteCellType;
            [_deleteButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        return _deleteButton;
    }
    
    - (void)buttonAction:(UIButton *)button
    {
        if (self.slectedActionBlock) {
            self.slectedActionBlock(button.tag, self);
        }
    }
    

    总结

    以上实现自定义的方法的关键点在于遍历到Cell底部的视图, 这种遍历的方法可能会随着iOS手机系统的版本不同而Cell层次结构也不同, 因此在最新的iOS10.2以后的版本中可能存在遍历不到的情况, 经过亲自测试,目前最新的iOS10.2以下的版本可用上面的方法亲测有效;

    相关文章

      网友评论

        本文标题:用系统方法自定义滑动Cell 视图(兼容iOS7)

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