不等高cell

純代碼創(chuàng)建

frame

  • 一.給模型增加frame數(shù)據(jù)
  • 所有子控件的frame
  • cell的高度
@interface Status : NSObject
/**** 文字\圖片數(shù)據(jù) ****/
// .....

/**** frame數(shù)據(jù) ****/
/** 頭像的frame */
@property (nonatomic, assign) CGRect iconFrame;
// .....
/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;
@end
  • 二.重寫(xiě)模型cellHeight屬性的get方法
- (CGFloat)cellHeight
{
    if (_cellHeight == 0) {
        // ... 計(jì)算所有子控件的frame、cell的高度
    }
    return _cellHeight;
}
  • 三.在控制器中
  • 實(shí)現(xiàn)一個(gè)返回cell高度的代理方法
    • 在這個(gè)方法中返回indexPath位置對(duì)應(yīng)cell的高度
/**
 *  返回每一行cell的具體高度
 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Status *status = self.statuses[indexPath.row];
    return status.cellHeight;
}
  • 四. 給cell傳遞模型數(shù)據(jù)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID = @"tg";
    // 訪(fǎng)問(wèn)緩存池
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    // 設(shè)置數(shù)據(jù)(傳遞模型數(shù)據(jù))
    cell.status = self.statuses[indexPath.row];

    return cell;
}
  • 五 新建一個(gè)繼承自UITableViewCell的子類(lèi),比如StatusCell
@interface StatusCell : UITableViewCell
@end
  • 六 在StatusCell.m文件中
  • 重寫(xiě)-initWithStyle:reuseIdentifier:方法
    • 在這個(gè)方法中添加所有需要顯示的子控件
    • 給子控件做一些初始化設(shè)置(設(shè)置字體、文字顏色等)
/**
 *  在這個(gè)方法中添加所有的子控件
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        // ......
    }
    return self;
}
  • 七 在StatusCell.h文件中提供一個(gè)模型屬性,比如Tg模型
@class Status;

@interface StatusCell : UITableViewCell
/** 團(tuán)購(gòu)模型數(shù)據(jù) */
@property (nonatomic, strong) Status *status;
@end
  • 八 在TgCell.m中重寫(xiě)模型屬性的set方法
  • 在set方法中給子控件設(shè)置模型數(shù)據(jù)
- (void)setStatus:(Status *)status
{
    _status = status;

    // .......
}
  • 九 重寫(xiě)-layoutSubviews方法
  • 一定要調(diào)用[super layoutSubviews]
  • 在這個(gè)方法中設(shè)置所有子控件的frame
/**
 *  在這個(gè)方法中設(shè)置所有子控件的frame
 */
- (void)layoutSubviews
{
    [super layoutSubviews];

    // ......
}

storyBoard創(chuàng)建

對(duì)比自定義等高cell,需要幾個(gè)額外的步驟(iOS8開(kāi)始才支持)

  • 添加子控件和contentView之間的間距約束
  • 設(shè)置tableViewCell的真實(shí)行高和估算行高
// 告訴tableView所有cell的真實(shí)高度是自動(dòng)計(jì)算(根據(jù)設(shè)置的約束來(lái)計(jì)算)
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 告訴tableView所有cell的估算高度
self.tableView.estimatedRowHeight = 44;
  • 根據(jù)文段最后底部結(jié)尾,可以利用autolayout的約束進(jìn)行控制,
    設(shè)置與底部的長(zhǎng)度的約束,如果是圖片的話(huà),要想不顯示圖片,可以利用約束控制這圖片的大小,注意如果是label的話(huà),控制大小成線(xiàn)也會(huì)有些影響.
    if (status.picture) { // 有配圖
        self.pictureHeight.constant = 100;
        self.pictureBottom.constant = 10;
        self.pictureImageView.image = [UIImage imageNamed:status.picture];
    } else { // 沒(méi)有配圖
        // 設(shè)置圖片高度為0
        self.pictureHeight.constant = 0;
        // 設(shè)置圖片底部間距為0
        self.pictureBottom.constant = 0;

如果要支持iOS8之前

  • 一.如果cell內(nèi)部有自動(dòng)換行的label,需要設(shè)置preferredMaxLayoutWidth屬性
- (void)awakeFromNib
{
    // 手動(dòng)設(shè)置文字的最大寬度(目的是:讓label知道自己文字的最大寬度,進(jìn)而能夠計(jì)算出自己的frame)
    self.text_label.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;
}
  • 二.設(shè)置tableView的cell估算高度
// 告訴tableView所有cell的估算高度(設(shè)置了估算高度,就可以減少tableView:heightForRowAtIndexPath:方法的調(diào)用次數(shù))
self.tableView.estimatedRowHeight = 200;
  • 三在代理方法中計(jì)算cell的高度
StatusCell *cell;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 創(chuàng)建一個(gè)cell(cell的作用:根據(jù)模型數(shù)據(jù)布局所有的子控件,進(jìn)而計(jì)算出cell的高度)
    if (!cell) {
        cell = [tableView dequeueReusableCellWithIdentifier:ID];
    }

    // 設(shè)置模型數(shù)據(jù)
    cell.status = self.statuses[indexPath.row];

    return cell.height;
}

- (CGFloat)height
{
    // 強(qiáng)制布局cell內(nèi)部的所有子控件(label根據(jù)文字多少計(jì)算出自己最真實(shí)的尺寸)
    [self layoutIfNeeded];

    // 計(jì)算cell的高度
    if (self.status.picture) {
        return CGRectGetMaxY(self.pictureImageView.frame) + 10;
    } else {
        return CGRectGetMaxY(self.text_label.frame) + 10;
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 我們?cè)谏弦黄锻ㄟ^(guò)代碼自定義不等高cell》中學(xué)習(xí)了tableView的相關(guān)知識(shí),本文將在上文的基礎(chǔ)上,利用sto...
    啊世ka閱讀 1,533評(píng)論 2 7
  • 一.界面搭建 1.確定開(kāi)發(fā)模式 如果界面是固定的,可以用xib界面的一些內(nèi)容不固定,就用純代碼cell用什么方式去...
    尕小天閱讀 591評(píng)論 5 9
  • ## 精華界面不等高cell的搭建 1 . 精華界面搭建,tableview展示數(shù)據(jù); 不等高cell(復(fù)雜界面)...
    龍龍_龍閱讀 692評(píng)論 1 5
  • 一.commentView模塊搭建 commentView樣式分為兩種 1.xib搭建界面 1.1 因?yàn)樵u(píng)論的樣式...
    尕小天閱讀 557評(píng)論 1 6
  • 自定義等高的cell 等高的cell 所有cell的高度是一樣的 純代碼創(chuàng)建 frame 1,新建一個(gè)繼承自UIT...
    LHsunshine閱讀 286評(píng)論 0 0