簡體   English   中英

如何基於可變高度UITableView中的內容確定UIWebView高度?

[英]How to determine UIWebView height based on content inside a variable height UITableView?

我有一個UITableView單元格,該單元格的大小將取決於其內容(可能是幾行文本)。

由於似乎在布局單元格之前會調用heightForRowAtIndexPath ,所以我只是通過在文本字符串上調用[NSString sizeWithFont]來猜測正確的高度。 在單元格中放置文本並確定其確切大小后,是否有更好的方法來設置高度?

我的問題是每個單元格包含一個UIWebView ,該UIWebView具有不同的(靜態加載的)內容,我無法弄清楚如何根據該內容計算合適的高度。 有沒有辦法做到這一點? 我已經嘗試過像這樣的事情:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       WebViewCell *cell = (WebViewCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];
       [cell setNeedsLayout];
       [cell layoutIfNeeded];
       return cell.bounds.size.height;
    }

單元格本身是從筆尖加載的,該筆尖只是一個包含UIWebViewUITableViewCell (雖然可變高度會更好,但是如果單元格只是將自身調整為最大的html內容也可以)。

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath { 
      UITableViewCell   *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath]; 
      return cell.bounds.size.height; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM