【发布时间】:2016-06-01 22:04:37
【问题描述】:
因此,我从 API 中提取了一个 HTML 字符串,并尝试将其加载到 UITableViewCell 中的 UIWebView 中,并且内容在单元格中滚动。我需要让 webview 的高度适应内容的大小,然后相应地调整单元格。我在这里迅速找到了一个帖子: UITableViewCell With UIWebView Dynamic Height 我尝试做同样的事情,但我遇到了一些麻烦,因为我使用 NSMutableArray 来存储带有浮点数的 NSNumber,然后尝试将其加载,但 cellForRowAtIndexPath 和 heightForRowAtIndexPath 都在调用 webview 委托方法之前执行,所以我会得到一个索引超出范围错误。
到目前为止,我有这个:
@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
提前致谢!
【问题讨论】:
-
你能添加一些代码来帮助你吗?
-
@ReinierMelian 添加了。谢谢!
-
我想知道您是否可以使用动态单元格高度,并使用自动布局约束来调整单元格的大小。在您的
webViewDidFinishLoad中,您可以更新constant上相应单元格的高度约束。可能比存储高度更容易。 -
@DylanS 我不确定我明白你的意思......我知道我怎么能做到这一点。我正在使用动态单元格高度,但我在 webview 完成加载时不能做太多事情,因为如果我在高度更新后将重新加载数据放在那里,它将导致循环。
-
你不想在那个方法中
reloadData。使用动态单元格高度,您可以随时调整单元格高度,表格将更新。您可以向单元格添加自动布局约束,使其高度必须等于 Web 视图高度吗?然后您可以在webViewDidFinishLoad中更新网络视图的高度。您还需要删除heightForRowAtIndexPath的实现,以便表格视图使用单元格的高度。
标签: ios objective-c uitableview uiwebview