iOS 将单元格高度设置为 UIWebView 的高度

标签 ios swift tableview

我有一个 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/

相关文章:

ios - 如何在 iOS 中编码流式视频?

swift - 使最后一行的底部成为表格 View 的底部(如果行数足够)

ios - TableView 单元格中的 UITextField 显示问题

ios - iOS 中 NSURLConnection 的间歇性 SSL 错误

ios - 未在导航栏内调用 UIButton 选择器

ios - 添加 Facebook 点赞按钮 iOS SWIFT

swift - 当使用 < 运算符时,Swift 在退出之前会遍历指定的数字

ios - 如何使用 AVAudioEngine 取消或删除回声/重复声音?

ios - 没有更改部分信息尽管更改导致 controllerDidChangeContent 调用 tableView.reloadData() 并依次调用 numberOfSections

ios - 如何在 Swift 中的自定义键盘中启用表情符号