[轉] iOS TableViewCell 動態調整高度

來源:互聯網
上載者:User

標籤:

原文: http://blog.csdn.net/crayondeng/article/details/8899577

最近遇到了一個cell高度變化的問題,在找解決辦法的時候,參考了這篇文章,覺得不錯

在寫sina 微博的顯示微博內容時,用到cell進行顯示,那麼就要考慮到不同微博內容導致的cell高度問題。在微博顯示的內容中包括了文字和圖片,那麼就要計算文字部分的高度和圖片部分的高度。這篇博文就記錄一下如何處理cell高度的動態調整問題吧!

一、傳統的方法

在 tableview的delegate的設定高度的方法中進行設定- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath,當然在這個代理方法中需要計算文字的高度以及圖片的高度,然後返回。

1、文字(string)高度的處理;

由於文字的長度的不確定的,所以就要根據這個動態文字長度來電腦顯示文字的的高度

[cpp] view plaincopy
  1. #define FONT_SIZE 14.0f  
  2. #define CELL_CONTENT_WIDTH 320.0f  
  3. #define CELL_CONTENT_MARGIN 10.0f  
  4.   
  5. NSString *string = @"要顯示的文字內容";  
  6. CGSize size = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAXFLOAT);  
  7. CGSize textSize = [string sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];  


以上代碼就是計算文字高度。其中size是裝載文字的容器,其中的height設定為maxfloat;然後用方法
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(UILineBreakMode)lineBreakMode
Returns the size of the string if it were rendered with the specified constraints.就可以知道string的size了


2、圖片高度的處理;
首先你先要下載到圖片,然後CGSize imageViewSize = CGSizeMake(image.size.width,image.size.height);就可以擷取到圖片的size,就可以對你的imageview的frame進行設定了,那麼也就知道了圖片的高度了。

二、非傳統方法
在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath代理方法中處理cell的frame的高度,在tableview的delegate的設定高度的方法中調用這個方法,那麼就可以得到設定好的cell高度。(注意到二者方法的執行順序:heightForRowAtIndexPath這個代理方法是先執行的,後執行cellForRowAtIndexPath)

[cpp] view plaincopy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellIdentifier = @"Cell";  
  4.       
  5.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];  
  6.     if (cell == nil) {  
  7.         //cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];  
  8.         //這個方法已經 Deprecated  
  9.           
  10.         cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];  
  11.           
  12.     }  
  13.     CGRect cellFrame = [cell frame];  
  14.     cellFrame.origin = CGPointMake(0, 0);  
  15.   
  16. //擷取cell的高度的處理  
  17.   
  18.     cellFrame.size.height = ***;  
  19.     [cell setFrame:cellFrame];  
  20.     return cell;  
  21.       
  22. }  


稍微解釋一下,注意到cell初始化的時候是CGRectZero,然後[cell frame]擷取cell的frame,讓後就可以對cell的高度進行處理後,setFrame 重新設定cell 的frame了。
接下來就是在heightForRowAtIndexPath這個方法中調用。

[cpp] view plaincopy
    1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {  
    2.   
    3.     UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];  
    4.     return cell.frame.size.height;  

[轉] iOS TableViewCell 動態調整高度

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.