我正在创建一个基于表的应用程序,需要为每个表格单元格显示一个UIWebView。我已经尝试过为表格中的每个单元格保留一个NSMutableArray高度,但它不起作用。
这是我的代码:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
aWebView.scrollView.scrollEnabled = NO;
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
if (arrayRowHeights.count == 0 || arrayRowHeights == nil) {
arrayRowHeights = [[NSMutableArray alloc] init];
[arrayRowHeights addObject:[NSString stringWithFormat:@"%f", fittingSize.height]];
}
else{
[arrayRowHeights addObject:[NSString stringWithFormat:@"%f", fittingSize.height]];
if (arrayRowHeights.count == aWebView.tag) {
[tblEventInfo reloadData];
}
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrayEventInfo count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [[arrayRowHeights objectAtIndex:indexPath.section] floatValue];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
EventInfoDetail *event = [[EventInfoDetail alloc] init];
event = [arrayEventInfo objectAtIndex:indexPath.section];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
webViewHTML = [[UIWebView alloc]init];
webViewHTML.delegate = self;
webViewHTML.tag = arrayEventInfo.count;
webViewHTML.backgroundColor = [UIColor clearColor];
webViewHTML.opaque = NO;
webViewHTML.userInteractionEnabled = NO;
if (event.d_description == (NSString *)[NSNull null]){
[webViewHTML loadHTMLString:@"" baseURL: nil];
}
else{
[webViewHTML loadHTMLString:event.d_description baseURL: nil];
}
webViewHTML.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);
[cell.contentView addSubview:webViewHTML];
}
return cell;
}
我的问题是我无法正确设置单元格的高度,任何人都可以找出我在这个给定代码上的错误。谢谢!
答案 0 :(得分:0)
每个单元格的webViewDidFinishLoad只有在加载表格单元后才会被调用i,e;所以序列调用将是 heightForRowAtIndexPath - > cellForRowAtIndexPath - > webViewDidFinishLoad。因此,您将永远无法在单元格加载之前设置高度。加载webView后尝试调用
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{[tableView reloadRowsAtIndexPaths:[tableView indexPathsForVisibleRows]
withRowAnimation:UITableViewRowAnimationNone];}
将再次调用heightForRowAtIndexPath并尝试再次设置它。 但是高度会在运行时增加。您应该自定义该事件
答案 1 :(得分:0)
预先将arrayRowHeights初始化为NSMutableArray(例如在viewDidLoad方法中),其容量与arrayEventInfo相同。将webView委托方法更改为此方法
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat height = aWebView.scrollView.contentSize.height;
if (height != [[arrayRowHeights objectAtIndex: aWebView.tag] floatValue]) {
[arrayRowHeights insertObject:[NSNumber numberWithFloat: height] atIndex: aWebView.tag];
[tblEventInfo reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:0 inSection:aWebView.tag]] withRowAnimation:UITableViewRowAnimationNone];
}
}
方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
更改行
webViewHTML.tag = arrayEventInfo.count;
带行
webViewHTML.tag = indexPath.section;