【发布时间】:2015-07-30 14:04:52
【问题描述】:
我正在努力调整包含动态高 WebView 的 UITableViewCell 的高度。
- (void) viewDidLoad {
// ...
for (ListItem *item in self.items) {
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectZero];
[webView loadHTMLString:item.content baseURL:nil];
[self.webViews addObject: webView]
}
// ...
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// try to calculate height?
return 100.0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
ListItem *listItem = (self.items) [indexPath.row];
[tableView registerNib:[UINib nibWithNibName:@"AccordionCell" bundle:nil] forCellReuseIdentifier:@"MyCustomCell"];
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCustomCell"];
// [...] do something with cell
UIWebView *webView = [self.webViews objectAtIndex: indexPath.row];
webView.frame = CGRectMake(0, 0, cell.mainView.frame.size.width, 0);
[cell.mainView addSubview: webView];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *height = [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('mainframe').offsetHeight;"];
// [...] adjust height of web view by using the content's height
}
但是,由于有多个线程,webViewDidFinishLoad 总是在cellForRowAtIndexPath 之后被调用,并且尝试通过更改webViewDidFinishLoad 中的框架来调整单元格高度到目前为止没有成功。
【问题讨论】:
标签: ios objective-c uitableview uiwebview