因此,我从 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/