gpt4 book ai didi

ios - iOS 中包含 UIWebView 的单元格的动态 UITableViewCell 高度

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:26:12 32 4
gpt4 key购买 nike

我想做的是将一些 HTML 资源文件加载到 UITableView 单元格中。我想根据 UIWebView 的内容大小调整单元格高度。以下是我的一些测试代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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>

现在我的输出如下图所示。

enter image description here

正如我在顶部所解释的,我的网页不包含任何 JavaScript。所以我可能无法使用 stringByEvaluatingJavaScriptFromString 方法返回文本。

最佳答案

试试这个

- (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*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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)webViewDidFinishLoad:(UIWebView *)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)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat height = [arrOfHTMLList[indexPath.section][@"height"] intValue];
return height;
}

希望对您有所帮助。

关于ios - iOS 中包含 UIWebView 的单元格的动态 UITableViewCell 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41277161/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com