iOS常用很常111

1. IOS 自定义可编辑Cell最优雅的方式

2021-03-06  本文已影响0人  ProfessorFan

问题

如何自定义可以编辑选中Cell还有简单的动画效果的方式(不用系统原始的编辑方式)

代码地址

GithubDemo地址

效果演示

GIF 耐心看完,一般的编辑功能也就这些东西了


EditCell效果图.gif

查看目录结构

编辑cell 目录结构.jpeg

实现思路,关键代码

  1. 首先自定一个cell
    自定义cell 这个就不做参数,不明白的可以直接下载demo 看。
  2. 计算cell 的frame
# addChildView 里面最关键的代码: [self.contentView layoutIfNeeded];一定要有,这个是动画效果实现的关键,有了这句代码,初始的cell 里面空间才会有frame ,这样动画效果才是最逼真的。
- (void)addChildView{
    
    [self.contentView addSubview:self.selectBtn];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(20);
        make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.selectBtn.mas_trailing).mas_offset(2);
        make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
        make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-15);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-15);
    }];
    
    [self.contentView addSubview:self.bottomLineView];
    [self.bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(20);
        make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-20);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(0.5);
    }];
    

    [self.contentView layoutIfNeeded];

}

  1. 添加动画效果
# 关键代码看  setModel 方法里面的 对 选中按钮的隐藏,内容的布局,还最简单的addAnimation 动画函数代码
- (void)setModel:(FanEditCellModel *)model{
    _model = model;
    
    if (model.isEditStatu) {
        self.selectBtn.hidden = NO;
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(self.selectBtn.mas_trailing).mas_offset(4);
            make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
            make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-15);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-15);
        }];
        
    } else{
        self.selectBtn.hidden = YES;
        [self.contentLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(self.contentView.mas_leading).mas_offset(20);
            make.top.mas_equalTo(self.contentView.mas_top).mas_offset(15);
            make.trailing.mas_equalTo(self.contentView.mas_trailing).mas_offset(-15);
            make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-15);
        }];
    }
    
    self.selectBtn.selected = model.isSelect;
    self.contentLabel.text = model.content;
    
    [self addAnimation];
}



- (void)setIsSelect:(BOOL)isSelect{
    _isSelect = isSelect;
    
    self.selectBtn.selected = isSelect;
    
}

- (void)addAnimation{
    [UIView animateWithDuration:0.25 animations:^{
        [self.contentView layoutIfNeeded];
    }];
}
  1. 至于选中和删除这个逻辑处理,这个简单不做叙述,给你一个Model
# FanBaseModel 只是一个基础model 你就当做NSObject
@interface FanEditCellModel : FanBaseModel

@property (nonatomic,assign)BOOL isSelect;

@property (nonatomic,strong)NSString *content;

/// YES :表示现在显示管理   NO:表示现在显示完成
@property (nonatomic,assign)BOOL isEditStatu;

@end
上一篇 下一篇

猜你喜欢

热点阅读