UITableView-09自定义等高的cell-Masonry方式

UITableView-01基本使用
UITableView-02模型优化
UITableView-03复杂的Plist解析
UITableView-04常见属性和样式
UITableView-05可重复使用Cell-有限的创建新的cell
UITableView-06可重复使用cell-注册方式
UITableView-07索引条(综合小案例)
UITableView-08自定义等高的cell-纯代码frame方式
UITableViewController-01简单使用

MasonryautoLayout的第三方框架具体使用,参考!
Masonry的使用

使用Masonry方式自定义等高的cell和UITableView-08自定义等高的cell-纯代码frame方式类似.
只是在自定义cell的位置和大小,有所不同而已!

//子控件的位置和约束
- (void)layoutSubviews{
    [super layoutSubviews];
    CGFloat space = 10; //间隙
    //图片的约束
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(space); //imgTemp的上边和左边距离父控件的上边和左边的距离10.
        make.bottom.offset(-space);
        make.width.equalTo(@80);//imgTemp的宽度80;
    }];
....
}

使用AutoLayout方式,可以直接将约束放在初始化方法里面.

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nullable NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        //图片
        UIImageView * imgTemp = [[UIImageView alloc]init];
        [self.contentView addSubview:imgTemp];
        self.iconImageView = imgTemp;
        CGFloat space = 10; //间隙
        //图片的约束
        [imgTemp mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.offset(space); //imgTemp的上边和左边距离父控件的上边和左边的距离10.
            make.bottom.offset(-space);
            make.width.equalTo(@80);//imgTemp的宽度80;
        }];
  ....
}
    return self;
}

注册方法,重复使用cell

- (void)viewDidLoad {
    [super viewDidLoad];
    //注册方式
    [self.tableView registerClass:[TGCell class] forCellReuseIdentifier:ID];
}
//每行显示的数据.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //根据ID,去缓存池去对应cell
    TGCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //将数据传递给重写的模型,实现给cell的子控件赋值
    cell.groupBuy = self.tgData[indexPath.row];
    return cell;
}

字典转模型- KVO 方式获赋值

+(instancetype)groupBuyWithDict:(NSDictionary*)dict{
    GroupBuy *groupBuy = [[self alloc] init];
    [groupBuy setValuesForKeysWithDictionary:dict];
    return groupBuy;
}

你可能感兴趣的:(UITableView-09自定义等高的cell-Masonry方式)