手動(dòng)計(jì)算 tableViewCell 高度與 AutoLayout Cell 高度計(jì)算的結(jié)合

手動(dòng)計(jì)算 tableViewCell 高度與 AutoLayout Cell 高度計(jì)算的結(jié)合

今天寫項(xiàng)目遇到一個(gè)特別坑爹的問(wèn)題, 就是之前的同事是使用純手碼計(jì)算 tableViewCell 的高度, 今天有個(gè)需求, 在原來(lái)的 tableView 上添加5個(gè)不同類型的 Cell, 我想這很容易啊, 直接 Xib 計(jì)算不就 OK 了,再結(jié)合 iOS8 的根據(jù) AutoLayout自動(dòng)計(jì)算Cell高度, 分分鐘的時(shí), 沒(méi)想到坑爹的一幕發(fā)生了, 同事是用 frame 排列的之前的 Cell 的內(nèi)容, 而我是需要 Autolayout 的, 他重寫了 -tableView:heightForRowAtIndexPath 方法, 這在 iOS8中與 AutoLayout 結(jié)合根本不需要寫的, 我一下子無(wú)語(yǔ)了, 還好我之前看過(guò)在 iOS7 中怎么結(jié)合 AutoLayout 計(jì)算 Cell 的高度, 于是將它們結(jié)合了起來(lái),大致是這樣的:

同事之前的代碼:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PDProduct *product = [self.data objectAtIndex:indexPath.row];
    return [PDFinancingProductCell getCellHeightOfProduct:product showLikeView:NO];
}

我使用 Xib 創(chuàng)建出 Cell 設(shè)置好約束后, 同樣重寫了 heightForRowAtIndexPath: 方法

- (void)viewDidLoad {
        [super viewDidLoad];
        
        // iOS8 自動(dòng) AutoLayoutCell 高度    
        self.tableView.rowHeight = UITableViewAutomaticDimension;
        self.tableView.estimatedRowHeight = 200;
        
        // 注冊(cè) Xib Cell
        /// 底部3張圖片 Cell
        [self.tableView registerNib:[UINib nibWithNibName:@"ProThreeImageCell" bundle:nil] forCellReuseIdentifier:@"ProThreeImageCell"];
        /// 只有文字描述的 Cell
        [self.tableView registerNib:[UINib nibWithNibName:@"ProNoImageCell" bundle:nil] forCellReuseIdentifier:@"ProNoImageCell"];
        /// 左邊一張圖片的 Cell
        [self.tableView registerNib:[UINib nibWithNibName:@"ProOneLeftImageCell" bundle:nil] forCellReuseIdentifier:@"ProOneLeftImageCell"];
        /// 右邊一張圖片的 Cell
        [self.tableView registerNib:[UINib nibWithNibName:@"ProOneRightImageCell" bundle:nil] forCellReuseIdentifier:@"ProOneRightImageCell"];
        /// 底部只有一張圖片的 Cell
        [self.tableView registerNib:[UINib nibWithNibName:@"ProOneImageCell" bundle:nil] forCellReuseIdentifier:@"ProOneImageCell"];
        
        [self initSubViews];
}


- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PDProduct *product = [self.data objectAtIndex:indexPath.row];
    if (product.show_type == FinacingPro) {
        [PDFinancingProductCell getCellHeightOfProduct:product showLikeView:NO];
    } else if (product.show_type == ProShowStyleBottomeThreeImage) { // 這種類型的 Cell 底部顯示3長(zhǎng)圖片
        // 如果之前計(jì)算過(guò)高度, 直接從緩沖中取
        if (self.cellHeightCaches[indexPath]) {
            return self.cellHeightCaches[indexPath] floatValue];
        }
        ProThreeImageCell *threeImageCell = [tableView dequeueReusableCellWithIdentifier:@"ProThreeImageCell"];
        if (!threeImageCell) {
            threeImageCell = [ProThreeImageCell proThreeImageCellWithTableView:tableView identifier:@"ProThreeImageCell"];
            self.cellCaches[indexPath] = threeImageCell;
        }
        threeImageCell.product = product;
        CGFloat cellHeight = [threeImageCell getCellHeight];
        self.cellHeightCaches[indexPath] = @(cellHeight);
        return cellHeight;
    } else {
        ....    // 其他幾個(gè)不同的 XibCell
    }
}


在 ProThreeImageCell 中:

    
  + (instancetype)proThreeImageCellWithTableView:(UITableView *)tableView identifier:(NSString *)identifier {
      ProThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        NSArray *xibs = [[NSBundle mainBundle] loadNibNamed:@"ProThreeImageCell" owner:nil options:nil];
        cell = xibs[0];
      }
    return cell;
  }
// titleLabel 的高度不固定
- (CGFloat)getCellHeight {
    self.titleLabel.preferredMaxLayoutWidth = WIDTH_SCREEN -        self.bankIconImageView.bounds.size.width - self.toPersonOrPublicImageView.bounds.size.width - 20;
    [self.textLabel layoutIfNeeded];
    self.titleLabel.text = self.product.title;
    [self.contentView layoutIfNeeded];
    CGFloat cellHeight = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
 return cellHeight + 1.f;   // +1.f 注意
}

菜鳥的總結(jié), 愿得到眾大神的指點(diǎn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容