UITableviewCell高度自適應(yīng)

以前真的是太孤陋寡聞了,每次遇到tableviewcell中既包含長度不定的label還有時有時無的圖片是,對于高度的自適應(yīng),做起來簡直不能再令人抓狂了。后來才發(fā)現(xiàn)是自己的方法大錯特錯了,其實tableviewcell的高度自適應(yīng)真的很簡單。

一、在初始化UItableView的時候,寫上如下代碼

 // 1.設(shè)置行高為自動撐開
    tableView.rowHeight = UITableViewAutomaticDimension;
    // 2.設(shè)置一個估計的行高,只要大于0就可以了,但是還是盡量要跟cell的高差不多
    tableView.estimatedRowHeight = 100;
    // 3.走完了以上兩步就不需要走 UITableViewDelegate 返回行高的那個代理了

注意:當寫上以上代碼的時候,就不需要寫UITableViewDelegate返回行高的這個方法。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

二、自定義Cell的布局(最關(guān)鍵的部分)

我們用Masory來布局,相信大家對這都不陌生,就不細說了
1.搭建約束,規(guī)則:自上而下,自左而右
2.cell中的UIlabel不要強制設(shè)置他的寬高,讓UIlabel根據(jù)內(nèi)容自適應(yīng)
3.界面中,最上方的控件一定要以self.contentview.mas_top來設(shè)置,第二個控件以最上方控件的mas_bottom來設(shè)置,以此類推;直到最低下的控件,必須要以self.contentview.mas_bottom來設(shè)置。只有這樣才能達到高度自適應(yīng)的要求(這一步很關(guān)鍵,大家要細細體會)
4.中間的控件盡量不要設(shè)置高度,讓控件的內(nèi)容自己撐開。
5.自左而右也是按照同樣的邏輯。

三、舉例說明

我們要實現(xiàn)如下這樣的一個界面

周星馳.png

該界面的Cell的代碼如下:

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // 繪制cell
        // 1.創(chuàng)建所有的控件
        self.nameLabel = [[UILabel alloc] init];
        self.nameLabel.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.nameLabel];
        
        self.timeLabel = [[UILabel alloc] init];
        self.timeLabel.font = [UIFont systemFontOfSize:11];
        [self.contentView addSubview:self.timeLabel];
        
        self.imageView1 = [[UIImageView alloc] init];
        [self.contentView addSubview:self.imageView1];
        
        self.contentLabel = [[UILabel alloc] init];
        self.contentLabel.numberOfLines = 0;
        self.contentLabel.font = [UIFont systemFontOfSize:20];
        [self.contentView addSubview:self.contentLabel];
        
        // 2.搭建約束,規(guī)則:自上而下,自左至右
        [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            // 3.昵稱label在最上側(cè),因此設(shè)置一個距離self.contentView.mas_top的約束
            make.top.equalTo(self.contentView.mas_top).offset(20.0);
            // 4.昵稱label在最左側(cè),因此設(shè)置一個距離self.contentView.mas_left的約束
            make.left.equalTo(self.contentView.mas_left).offset(20.0);
            // 5.不要強制設(shè)置昵稱label的寬高,讓昵稱label根據(jù)內(nèi)容自動撐開
        }];
        
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            // 6.時間label在昵稱label的右側(cè)(假設(shè)在右側(cè),為了解釋左右布局)
            make.left.equalTo(self.nameLabel.mas_right).offset(20.0);
            make.bottom.equalTo(self.nameLabel.mas_bottom);
            make.right.lessThanOrEqualTo(self.contentView.mas_right).offset(-20.0);
        }];
        
        [self.imageView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.nameLabel.mas_left); // 與昵稱label左對齊
            make.top.equalTo(self.nameLabel.mas_bottom).offset(20.0);
#warning 可以設(shè)死寬和高,也可以不設(shè)。如果不設(shè)那么圖片會自動撐開
            make.size.mas_equalTo(CGSizeMake(100.0, 100.0));
        }];
        
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            // 內(nèi)容label在最下面,兩個約束是必須要設(shè)置的
            // 7.距離上面一個label的距離
            make.top.equalTo(self.imageView1.mas_bottom).offset(20.0);
            // 8.距離self.contentView.mas_bottom的距離
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-20.0);
            // 上面兩個步驟決定了內(nèi)容的高度
            
            make.left.equalTo(self.contentView.mas_left).offset(20.0);
            make.right.equalTo(self.contentView.mas_right).offset(-20.0);
        }];
        
        /*
         精髓:1.約束規(guī)則必須按照自上而下,自左至右的規(guī)則
         2.自上而下布局需要:下面的view的top與上面的view的bottom搭建約束,第一個view的top要與contentView的top搭建約束,最后一個的View的bottom要與contentView的bottom搭建約束
         3.中間盡量不要設(shè)置高度,讓label的內(nèi)容撐開
         4.自左至右也是按照同樣的邏輯
         */
        
    }
    
    return self;
}

方法很簡單,需要細細體會一下,你會有一種豁然開朗的感覺!!小小碼農(nóng)一枚,與大家分享工作中的小小經(jīng)驗!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容