我正在尝试将一些 HTML 资源文件加载到 UITableView 单元格中。我想根据 UIWebView 的内容大小调整单元格高度。以下是我的一些测试代码。
- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
static NSString *cellIdentifier = @"TableCellID";
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed"CustomTableViewCell" owner:self options:nil];
cell = [nibArray objectAtIndex:0];
}
NSString *htmlFileName = [NSString stringWithFormat"page%ld", (long)indexPath.row];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:htmlFileName ofType"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[cell.webView loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
return cell;
}
如果我描述了我的 CustomTableViewCell.xib,它在内容 View 中有一个 UIWebView,并且 UIWebView 有以下约束。
ContentView.Bottom = WebView.Bottom + 8
ContentView.Top = WebView.Top +8
ContentView.Leading = WebView.Leading + 8
ContentView.Trailing = WebView.Trailing + 8
HTML 文件没有 JavaScript 或 CSS 内容。简单如下。
<html>
<header>
</header>
<body>
<p>eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee eeeee</p>
</body>
</html>
现在我的输出如下图所示。
正如我在顶部所解释的,我的网页不包含任何 JavaScript。所以我可能无法使用 stringByEvaluatingJavaScriptFromString 方法来返回文本。
Best Answer-推荐答案 strong>
试试这个
- (void)viewDidLoad {
[super viewDidLoad];
arrOfHTMLList= [[NSMutableArray alloc] init];
NSMutableDictionary *dic1 = [[NSMutableDictionary alloc] init];
[dic1 setObject"0" forKey"height"];
[dic1 setObject"aaaaaaaaaa aaaaaa aaaaaaa aaaaaaa aaaaaa aaaaaa aaaaaaaa aaaaa" forKey"htmlString"];
NSMutableDictionary *dic2 = [[NSMutableDictionary alloc] init];
[dic1 setObject"0" forKey"height"];
[dic1 setObject"abababababbabababababab ababab bababa ababababb" forKey:@"htmlString"];
NSMutableDictionary *dic3 = [[NSMutableDictionary alloc] init];
[dic1 setObject:@"0" forKey:@"height"];
[dic1 setObject:@"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:@"htmlString"];
NSMutableDictionary *dic4 = [[NSMutableDictionary alloc] init];
[dic1 setObject:@"0" forKey:@"height"];
[dic1 setObject:@"sdajdkjas dkasjkhd jkashjkd sakjhdkj askjdaksh dksajkdkjsah djkasjkhd kjsahjkd asjkhdk jasjkdhaskj" forKey:@"htmlString"];
[arrOfHTMLList addObject:dic1];
[arrOfHTMLList addObject:dic2];
[arrOfHTMLList addObject:dic3];
[arrOfHTMLList addObject:dic4];
}
-(UITableViewCell*)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
UIWebView *webView = (UIWebView*)[cell viewWithTag:1];
NSString *myHTML = arrOfHTMLList[indexPath.section][@"htmlString"];
[webView loadHTMLString:myHTML baseURL:nil];
webView.delegate = self;
webView.frame = CGRectMake(0, 0, screenWidth,cell.frame.size.height);
return cell;
}
-(void)webViewDidFinishLoadUIWebView *)webView
{
for (UIView *parent=[webView superview];parent!=nil ;parent=[parent superview] )
{
if([parent isKindOfClass:[UITableViewCell class]])
{
UITableViewCell *cell=(UITableViewCell *)parent;
NSIndexPath *indexPath=[_tableView indexPathForCell:cell];
int height = webView.scrollView.contentSize.height;
[[arrOfHTMLList objectAtIndex:indexPath.section] setObject:[NSString stringWithFormat:@"%d",height] forKey:@"height"];
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath
{
CGFloat height = [arrOfHTMLList[indexPath.section][@"height"] intValue];
return height;
}
希望对你有帮助。
关于ios - iOS 中包含 UIWebView 的单元格的动态 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/41277161/
|