UITableViewCell 動(dòng)態(tài)高度計(jì)算 - 手動(dòng)計(jì)算控件高度

1.創(chuàng)建Xib,畫出Cell布局

2.創(chuàng)建Xib對(duì)應(yīng)的Cell類

#import <UIKit/UIKit.h>
@interface YCTableViewCell : UITableViewCell
+ (instancetype)CellWithTitle:(NSString *)text tableView:(UITableView *)tableView;
- (CGFloat)cellHeight;
@end 
#import "YCTableViewCell.h"

@interface YCTableViewCell ()
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@end

@implementation YCTableViewCell

+ (instancetype)CellWithTitle:(NSString *)text tableView:(UITableView *)tableView{
    YCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YCTableViewCell" owner:self options:nil] firstObject];
        cell.label.text = text;
    }
    return cell;
}

3.控制器

#import "YCViewController1.h"
#import "YCTableViewCell.h"

@interface YCViewController1 ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
/**
 *  文字?jǐn)?shù)組(cell數(shù)量)
 */
@property (nonatomic, strong) NSArray *titleArr;
/**
 *  緩存cell高度
 */
@property (nonatomic, strong) NSArray *rowHeightArr;
@end

@implementation YCViewController1
/**
 *  通過(guò)手動(dòng)計(jì)算Cell中每個(gè)控件的高度 得出Cell總高度
 */
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *title = self.titleArr[indexPath.row];
    YCTableViewCell *cell = [YCTableViewCell CellWithTitle:title tableView:tableView];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [self.rowHeightArr[indexPath.row] integerValue];
}

/**
 *  調(diào)用這個(gè)方法之后,會(huì)先執(zhí)行cellForRowAtIndexPath而不是heightForRowAtIndexPath
 */
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
    }
    return _tableView;
}

- (NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = @[@"二二二二二二二二二二二",@"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二",@"二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二二"];
    }
    return _titleArr;
}

/**
 *  計(jì)算Cell高度,并緩存
 *
 *  @return 返回緩高度數(shù)組
 */
- (NSArray *)rowHeightArr{
    NSMutableArray *tmpArr = [@[] mutableCopy];
    for (NSString *title in self.titleArr) {
        CGFloat marginH = 8;
        CGFloat bottomViewH = 30;
        CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX);
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.lineSpacing = 3.0;
        // 計(jì)算label文字的實(shí)際高度
        CGFloat labelH = [title boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17] , NSParagraphStyleAttributeName : style} context:nil].size.height;
        CGFloat height = labelH + marginH + bottomViewH + marginH;
        [tmpArr addObject:@(height)];
    }
    return tmpArr.copy;
}
@end
最后編輯于
?著作權(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)容

  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,618評(píng)論 30 472
  • 1.自定義控件 a.繼承某個(gè)控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,702評(píng)論 2 4
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,293評(píng)論 4 61
  • 該怎么說(shuō)起今天的感受呢?似乎很難起頭。 那就從早飯后和媽媽弟弟一起去看大姨媽開始吧。 大姨家住在周村坡,大姨大媽媽...
    七月紫蘇閱讀 258評(píng)論 0 0
  • 1、平平淡淡是真沒(méi)錯(cuò),只是在強(qiáng)勁撕裂和極度分化的社會(huì)發(fā)展進(jìn)程中,你的低風(fēng)險(xiǎn)承受和應(yīng)對(duì)的能力、你的低掌控自己命運(yùn)的能...
    黑麥的光影部落閱讀 293評(píng)論 0 0

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