ios - 带有 UIWebview 的 UITableViewCell,可根据内容大小调整高度

标签 ios objective-c uitableview uiwebview

因此,我从 API 中提取了一个 HTML 字符串,并尝试将其加载到 UITableViewCell 中的 UIWebView 中,并且内容在单元格中滚动。我需要让 webview 的高度适应内容的大小,然后相应地调整单元格。我在这里迅速找到了一个帖子:
UITableViewCell With UIWebView Dynamic Height
我尝试做同样的事情,但我遇到了一些麻烦,因为我使用 NSMutableArray 来存储带有浮点数的 NSNumber,然后尝试加载它,但是 cellForRowAtIndexPath 和 heightForRowAtIndexPath 都在调用 webview 委托(delegate)方法之前执行,所以我会得到一个索引越界错误。

到目前为止,我有这个:

@interface FGCThreadDetailTableViewController ()

@property (copy, nonatomic) NSArray<FGCThreadModel*>* threads;
@property (copy, nonatomic) NSMutableArray* contentHeights;

@end

@implementation FGCThreadDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[FGCThreadDetailCell class] forCellReuseIdentifier:@"threadDetailCell"];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    [SVProgressHUD show];
    [[FGCThread postsByThreadID:_threadID] subscribeNext:^(id x) {
        _threads = x;
        [SVProgressHUD dismiss];
        [self.tableView reloadData];
    }];
    _contentHeights = [[NSMutableArray alloc] init];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _threads.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FGCThreadDetailCell* cell = [tableView dequeueReusableCellWithIdentifier:@"threadDetailCell" forIndexPath:indexPath];
    FGCThreadModel* thread = _threads[indexPath.row];
    cell.webView.delegate = self;
    CGFloat htmlHeight;
    htmlHeight = [_contentHeights[indexPath.row] floatValue];
    cell.webView.tag = indexPath.row;
    [cell.webView loadHTMLString:thread.postBodyHTMLString baseURL:nil];
    cell.webView.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [_contentHeights[indexPath.row] floatValue];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (_contentHeights[webView.tag]) {
        return;
    }
    _contentHeights[webView.tag] = [NSNumber numberWithFloat:[[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]];
    [self.tableView reloadData];
}

@end

提前致谢!

最佳答案

自动布局方法可能很棘手。一种更简单的方法是在webViewDidFinishLoad 中设置相应单元格的框架。 .您可以使用 cellForRowAtIndexPath在表格 View 上获取显示的单元格(如果它已经显示,它不会尝试从 UITableViewDataSource 获取单元格,因为它将被缓存)。

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if (_contentHeights[webView.tag]) {
        return;
    }
    float height = [NSNumber numberWithFloat:[[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]];

    NSIndexPath* indexOfCell = [NSIndexPath indexPathForRow:webView.tag inSection:0];
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexOfCell];
    cell.frame = CGRectMake(0, 0, cell.frame.size.width, height);
}

关于ios - 带有 UIWebview 的 UITableViewCell,可根据内容大小调整高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37579554/

相关文章:

ios - 使用自动布局的 uitableview 页脚 View 的运行时扩展

ios - 如何在 Swift 中临时存储图像和字符串?

ios - 背景上的 SCNNode 大图像导致崩溃

ios - View (或图层)何时需要离屏渲染?

ios - FBSDKGraphRequest 不工作(iOS)

iphone - Objective C 中的多个字符串替换

iphone - iOS 的 HTTP 中的 getReasonPhrase()

ios - 如何向 uitableview 的每个单元格添加一个按钮?

ios - 在 iPhone 上的一个 UIWebView 中显示多个 PPT

ios - 为什么我的自定义 UITableViewCell 没有显示?