forkingdog / UITableView-FDTemplateLayoutCell

Template auto layout cell for automatically UITableViewCell height calculating
MIT License
9.94k stars 2.01k forks source link

使用Masonry 多种Cell,计算的高度都是45.333333333... #369

Open yubaokang opened 6 years ago

yubaokang commented 6 years ago

打印日志

2018-04-10 18:45:59.283675+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:0] - 45.33333333333334
2018-04-10 18:46:00.036209+0800 goujiawang[572:99942] Status bar could not find cached time string image. Rendering in-process.
2018-04-10 18:46:01.424444+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:1] - 45.33333333333334
2018-04-10 18:46:01.519299+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:0] - 45.33333333333334
2018-04-10 18:46:06.331406+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:1] - 45.33333333333334
2018-04-10 18:46:06.445699+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:0] - 45.33333333333334
2018-04-10 18:46:07.569814+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:3] - 45.33333333333334
2018-04-10 18:46:07.627344+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:2] - 45.33333333333334
2018-04-10 18:46:07.664490+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:1] - 45.33333333333334
2018-04-10 18:46:07.759185+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:0] - 45.33333333333334
2018-04-10 18:46:10.249493+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:2] - 45.33333333333334
2018-04-10 18:46:10.270484+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:1] - 45.33333333333334
2018-04-10 18:46:10.293437+0800 goujiawang[572:99942] ** FDTemplateLayoutCell ** hit cache by index path[0:0] - 45.33333333333334

代码 GJWHomeNewViewController.m

...
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GJWHomeModel *model=dataArray[indexPath.row];
    GJWHomeBaseCell *cell;
    cell=[tableView dequeueReusableCellWithIdentifier:[self getCellIdentifier:model.templateId]];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //使用Masonry进行布局的话,这里要设置为NO
    cell.fd_enforceFrameLayout = NO;
    [cell setModel:model];
    return cell;
}

//cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GJWHomeModel *model=dataArray[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier:[self getCellIdentifier:model.templateId] cacheByIndexPath:indexPath configuration:^(id cell) {
        [cell setModel:model];
    }];
}

-(NSString *)getCellIdentifier:(NSInteger)templateId{
    NSString *cellIdentifier;
    switch (templateId) {
//        case templateId_1001:
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
//        case templateId_1002:
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
//        case templateId_1006:
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
//        case templateId_1011:
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
//        case templateId_1013:
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
        case templateId_2101:
            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
            break;
//        case templateId_2102:
//            cellIdentifier=NSStringFromClass([GJWHomeOnePicCell class]);
//            break;
//        case templateId_2103://HTML - 2103
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
//        case templateId_2104:
//            cellIdentifier=NSStringFromClass([GJWHomeBannerCell class]);
//            break;
        default:
            cellIdentifier=NSStringFromClass([GJWHomeOnePicCell class]);
            break;
    }
    return cellIdentifier;
}

-(void)registerCell{
    [_tableView registerClass:[GJWHomeBannerCell class] forCellReuseIdentifier:NSStringFromClass([GJWHomeBannerCell class])];
    [_tableView registerClass:[GJWHomeOnePicCell class] forCellReuseIdentifier:NSStringFromClass([GJWHomeOnePicCell class])];
}
...

代码 GJWHomeBannerCell.m

...
-(void)layoutSubviews{
    self.contentView.backgroundColor=[UIColor greenColor];
    [self.contentView setTheHeight:200];
    [title makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
//        make.centerX.equalTo(self.contentView);
   }];
    [more makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(title.right);
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(title.bottom);
    }];
    [scrollView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(title.bottom);
        make.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
}
...

代码GJWHomeOnePicCell.m

...
-(void)layoutSubviews{
    self.contentView.backgroundColor=[UIColor redColor];
    [self.contentView setTheHeight:150];
    [title makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
//        make.centerX.equalTo(self.contentView);
    }];
    [more makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(title.right);
        make.top.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
    }];
    [img makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(title.bottom);
        make.right.equalTo(self.contentView);
//        make.height.equalTo(150);
        make.bottom.equalTo(self.contentView).offset(-5);
    }];
}
...
libaixiao commented 5 years ago

你现在这个问题解决了没?