美文网首页程序员
2022-09-05 解决自定义TableViewCell拖动

2022-09-05 解决自定义TableViewCell拖动

作者: 小专注 | 来源:发表于2022-09-05 17:36 被阅读0次
    用户触发按钮中的事件,回调给model状态,用户滑动cell时,复用池出现的cell会重新取model里的值
    前言:
    model 中添加一个属性
    @property(nonatomic, assign) BOOL selected;  //记录选中状态
    
    1. 声明Block
    @interface AAAddCell : UITableViewCell
    // step3-1: 声明block一个属性
    @property(nonatomic, copy) void (^addBlock)(BOOL isAdd);
    @end
    

    2.按钮事件中添加

     // step3-2-1: 当前页面 已 添加
            if (self.addBlock) {
                self.addBlock(YES);
            }
    
     // step3-2-1: 当前页面 取消 已添加
            if (self.addBlock) {
                self.addBlock(NO);
            }
    

    3:赋值

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 实现UITableViewCell的初始化 ,包含数据源转model 给cell 赋值
    
    
    // step3-3:滑动到复用时,展示Model中selected的状态
        addCell.addBlock = ^(BOOL isAdd) {
            if (isAdd) {
                model.selected = YES;
            }else{
                model.selected = NO;
            }
        };
        return addCell;
    }
    
    

    相关文章

      网友评论

        本文标题:2022-09-05 解决自定义TableViewCell拖动

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