基于Webview动态设置TableCell高度
Dynamically set TableCell height based on Webview
我正在努力调整包含动态高 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
中的框架来调整单元格高度到目前为止没有成功.
将方法 webViewDidFinishLoad:
中的高度存储在 NSMutableArray 中,然后最后重新加载 UITableView
的数据,并根据 indexPath.Row
在 heightForRowAtIndexPath
中使用这些高度.
我不想误导您,但是如果您需要将静态 HTML 内容打印到视图中,您可以使用 UILabel 和属性字符串。
像那样:
NSMutableAttributedString *attributedString;
NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
attributedString = [[NSMutableAttributedString alloc] initWithData:htmlData
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
someLabel.attributedText = attributedString;
如果你使用这个,你不需要等待 WebView 加载,你可以使用标准方法来计算高度。
我正在努力调整包含动态高 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
中的框架来调整单元格高度到目前为止没有成功.
将方法 webViewDidFinishLoad:
中的高度存储在 NSMutableArray 中,然后最后重新加载 UITableView
的数据,并根据 indexPath.Row
在 heightForRowAtIndexPath
中使用这些高度.
我不想误导您,但是如果您需要将静态 HTML 内容打印到视图中,您可以使用 UILabel 和属性字符串。
像那样:
NSMutableAttributedString *attributedString;
NSData *htmlData = [htmlString dataUsingEncoding:NSUTF8StringEncoding];
attributedString = [[NSMutableAttributedString alloc] initWithData:htmlData
options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
someLabel.attributedText = attributedString;
如果你使用这个,你不需要等待 WebView 加载,你可以使用标准方法来计算高度。