我有一个 TableView,在第一个单元格中我在 WebView 中显示 HTML 内容。
我希望此单元格具有 WebView 中内容的高度。
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if(indexPath.row == 0) {
let cell = articleTableView.dequeueReusableCellWithIdentifier("articleContentCell", forIndexPath: indexPath) as! ArticleContentTableViewCell
cell.articleContentWebView.loadHTMLString(newsItem.entry.content, baseURL: nil)
return cell
}
...
}
我如何计算这个?由于单元格尚未渲染,我什至可以在 CellForRowAtIndexPath 中计算它吗?我可以在 web View 中获取 ScrollView 高度,但它无法获取高度。
最佳答案
- (void) loadString (NSString *)Str{
[theWebView loadHTMLString:Str baseURL:nil];}
#pragma mark - UIWebView Delegate methods
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([self.delegate respondsToSelector
:@selector(updateCellHeight:currentSection:)]) {
CGRect frame = webView.frame;
frame.size = [webView sizeThatFits:CGSizeZero];
frame.size.height += 10.0f;
webView.frame = frame;
webView.delegate = nil;
[self.delegate updateCellHeight:webView.scrollView.contentSize.height currentSection:webView.tag];
}
}
#pragma mark - RichTextCellDelegate
- (void) updateCellHeight:(CGFloat)richTextHeight currentSection:(NSInteger)Section
{
if (![sectionArray containsObject:[NSNumber numberWithInteger:Section]]){
[sectionArray addObject:[NSNumber numberWithInteger:Section]];
NSLog(@"CurrentHeight%f",richTextHeight);
richTextCellHeight = richTextHeight;
savedIndexPath = Section;
//[theTableView reloadData]; // No Need to load Whole Table
NSIndexSet *indexes = [[NSIndexSet alloc] initWithIndex:Section];
[theTableView reloadSections:indexes withRowAnimation:UITableViewRowAnimationNone];
}
}
关于iOS 将单元格高度设置为 UIWebView 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32741821/