【iOS知識學習】_iOS動態改變TableView Cell高度

來源:互聯網
上載者:User

標籤:style   color   os   io   for   cti   代碼   div   

在做tableView的時候,我們有時候需要根據cell的高度動態來調整,最近在網上看到一段代碼不錯,跟大家Share一下。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

類中擷取cell的高度:
    CGSize boundSize = CGSizeMake(216, CGFLOAT_MAX);    cell.textLabel.text = @"12345678900123456789";    cell.userInteractionEnabled = NO;    cell.textLabel.numberOfLines = 0;    CGSize requiredSize = [cell.textLabel.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:boundSize lineBreakMode:UILineBreakModeWordWrap];    CGRect rect = cell.frame;    rect.size.height = requiredSize.height+5;    cell.frame = rect;
這時候擷取到了cell的高度,然後在

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

類中改變cell的高度:
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];        NSLog(@"cell height %f",cell.frame.size.height);    return cell.frame.size.height;

這樣以來cell的高度就根據cell裡label的內容自動改變啦。

其主要出發點就是我有一個label,然後我要把這個label展示出來,我根據字型的大小還有行數來擷取一個高度,這樣cell的高度就有啦。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.