UITableViewCell自適應(yīng)筆記

UITableView

創(chuàng)建UITableView之后,會(huì)先numberOfRowsAtIndexPaht和heightForRowAtIndexPath 計(jì)算大概高度 (因?yàn)橹蓝嗌贄l 每條高度多少 可以算出UIScrollView的contentSize一樣的東西)
(如果有100條)heightForRow就會(huì)走100次
然后cellForRowAtIndexPath顯示每個(gè)cell
比如:

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray arrayWithObjects:@"000", @"111", @"222", @"333", @"444", @"555", @"666", @"777", @"888", @"999", @"101010", nil];
    
    self.tblView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tblView.dataSource = self;
    self.tblView.delegate = self;
    [self.view addSubview:self.tblView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"------------%s", __func__);
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"++++++++%s+++++++++%ld", __func__, indexPath.row);
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseId"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseId"];
    }
    cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 100;
    NSLog(@"++++++++%s+++++++++%ld", __func__, indexPath.row);
    return height;
}


輸出的結(jié)果如下:

-------------[ViewController tableView:numberOfRowsInSection:]
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++0
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++1
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++2
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++3
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++4
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++5
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++6
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++7
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++8
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++9
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++10
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++0
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++0
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++1
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++1
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++2
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++2
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++3
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++3
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++4
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++4
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++5
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++5
++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++6
++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++6

可以看出執(zhí)行的順序.即使畫(huà)面只能顯示到666,heightForRow還是會(huì)執(zhí)行到10

屏幕快照 2016-03-15 下午7.12.08.png

非等高cell布局思路:

  • 在heightForRow里創(chuàng)建一個(gè)cell(注意是創(chuàng)建,不是調(diào)用cellForRow方法,否則會(huì)死循環(huán),因?yàn)閏ellForRow時(shí)候會(huì)調(diào)用heightForRow).布局一次(layoutIfNeeded) 得到高度之后 return 得到的高度.
    這是最low的方法, 因?yàn)樵趆eightForRow里創(chuàng)建了一個(gè)cell只為了得到cell的高度. 得到之后就干掉這個(gè)cell. 上面也說(shuō)了 如果有100條 會(huì)調(diào)用100次 創(chuàng)建100個(gè)cell只為獲得他的高度. 很耗性能.
  • 上面也介紹了如果cellForRow那一定要先知道cell的高度.其實(shí)我們可以改變一下順序的 蘋(píng)果的API有提供一個(gè)方法
// Use the estimatedHeight methods to quickly calcuate guessed values which will allow for fast load times of the table.
// If these methods are implemented, the above -tableView:heightForXXX calls will be deferred until views are ready to be displayed, so more expensive logic can be placed there.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(7_0);

這個(gè)方法會(huì)讓tableview估算一個(gè)cell的值(別太小的離譜,大概200 - 300),因?yàn)橛泄浪愕闹?所以會(huì)先走cellForRow,然后tableview覺(jué)得估算的height不靠譜,這時(shí)才會(huì)heightForRow.因?yàn)檫@時(shí)候cellForRow已經(jīng)走完了, 可以在cellForRow中拿到cell的高度(cellForRow只是賦值,不會(huì)布局,記得layoutIfNeeded,這樣才會(huì)走cell的layoutSubviews),保存起來(lái)(用數(shù)組或者字典都可以),然后在heightForRow里根據(jù)保存的值顯示出來(lái).
而且因?yàn)橛辛斯浪愕腸ell高度,所以即使有100條,也不會(huì)再最開(kāi)始就走100次heightForRow了,也算是對(duì)性能稍微優(yōu)化了一些.

// 修改每個(gè)cell高度不相等
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat height = 40 + 10 * indexPath.row;
    NSLog(@"++++++++%s+++++++++%ld", __func__, indexPath.row);
    return height;
}
// 增加估算的cell高度
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 240;
}

輸出結(jié)果:

 -------------[ViewController tableView:numberOfRowsInSection:]
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++0
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++0
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++1
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++1
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++2
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++2
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++3
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++3
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++4
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++4
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++5
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++5
 ++++++++-[ViewController tableView:cellForRowAtIndexPath:]+++++++++6
 ++++++++-[ViewController tableView:heightForRowAtIndexPath:]+++++++++6
  • 在上面2方法中 需要再建立個(gè)數(shù)組或者字典存放cell的高度,有些麻煩.
    因?yàn)檎R粋€(gè)cell應(yīng)該是對(duì)應(yīng)一個(gè)數(shù)據(jù)模型,所以可以將cell的高度存放在模型中 然后在heightForRow里去獲取model.cellHeight.

另外:xib自動(dòng)布局中 UILabel比較特殊 設(shè)置寬度約束之后可以不設(shè)置高度約束,也可以自動(dòng)顯示出UILabel. 這種情況下可能cell里的label下面可能會(huì)有一個(gè)空行(多行的自動(dòng)換行時(shí)候可能會(huì)有).這時(shí)最好告訴label最大寬度(label.preferredMaxLayoutWidth屬性)
因?yàn)閘ayoutIfNeeded是強(qiáng)制布局,強(qiáng)制布局時(shí)候如果不準(zhǔn)確的告訴程序文字的最大寬度label.preferredMaxLayoutWidth 可能估算的最大高度可能會(huì)有偏差 所以可能會(huì)造成label下面可能會(huì)有一個(gè)空行的問(wèn)題

最后貼一下代碼:
模型:

@interface DataModel : NSObject

@property (nonatomic, copy)NSString *textValue;
@property (nonatomic, assign)CGFloat cellHeight;

@end

cell:

@interface MyTableViewCell : UITableViewCell

@property (nonatomic, strong)DataModel *model;

@end
@implementation MyTableViewCell

-(void)setModel:(DataModel *)model {
    _model = model;
    
    self.textLabel.text = _model.textValue;
    // 這里偷懶了, 正常應(yīng)該是文字或者圖片的自適應(yīng)高度
    CGFloat height = 40 + 10 * [_model.textValue integerValue] * 0.01;
    
    // 布局之后獲取高度
    [self layoutIfNeeded];

    model.cellHeight = height;
}

@end

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];
    
    for (NSInteger i = 0; i <= 10; i++) {
        DataModel *data = [[DataModel alloc] init];
        data.textValue = [NSString stringWithFormat:@"%ld%ld%ld", i, i, i];
        [self.array addObject:data];
    }
    
    self.tblView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tblView.dataSource = self;
    self.tblView.delegate = self;
    [self.view addSubview:self.tblView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSLog(@"------------%s", __func__);
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"++++++++%s+++++++++%ld", __func__, indexPath.row);
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseId"];
    if (!cell) {
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseId"];
    }
    cell.model = [self.array objectAtIndex:indexPath.row];
    return cell;
}

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

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 240;
}

結(jié)果圖:

屏幕快照 2016-03-15 下午7.38.06.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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