1

我需要在我的 Uitableview 的每个 tableviewcell 中显示每个 webview。使用下面的代码时,当有 2 个元素时,第一个单元格是空的,但第二个单元格是正确的。

hrs 和 hrsHtml 包含所有值,问题是只有最后一个数据显示在 tableview 的相应单元格中。其他单元格只是空白。

同样是总单元格为 2,首先我们只能看到第二个单元格,但是在滚动表格视图重新加载后,第二个单元格消失并显示第一个单元格。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return [brId count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {
        cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    [cell.contentView addSubview:hrs];

    hrsHtml = [NSString stringWithFormat:@"  <font size=\"2\"  face=\"Arial\">%@  </font>",[html objectAtIndex:indexPath.row]];

    [hrs loadHTMLString:hrsHtml baseURL:nil];

    return cell;
}

出现tableview时的屏幕截图,单元格2中只有webview

在此处输入图像描述

tableview滚动时的截图,只有单元格1中的webview,单元格2消失

在此处输入图像描述

4

3 回答 3

1

由于hrshrsHtml是单个对象,即使您有多个单元格,您也只有一个对象。如果您修改hrs,它将为所有单元格更改,因为它们似乎正在共享它。(除非您在某处有其他代码可以更改这些变量指向的对象。)

另一个奇怪的事情是,您使用brId数组来确定行数并使用html数组来获取行内容。如果它们不同步,您将遇到问题。

此外,您应该只在创建新单元格时向单元格添加子视图。

于 2012-08-17T11:45:02.417 回答
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{


    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;
    }

    hrs = [[UIWebView alloc] initWithFrame:CGRectMake(10,0,320,84)];

    hrs.userInteractionEnabled = YES;

    hrs.backgroundColor = [UIColor clearColor];

    hrsHtml = [NSString stringWithFormat:@"  <font size=\"2\"  face=\"Arial\">%@  </font>",[html objectAtIndex:indexPath.row]];

    [hrs loadHTMLString:hrsHtml baseURL:nil];

    [cell.contentView addSubview:hrs];

    hrsHtml = nil;

    return cell;

}

现在 webview 在每个 tableviewcell 中正确加载。

于 2012-08-21T05:29:34.113 回答
0

我认为您在初始化单元格时遇到问题尝试下面的代码

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
于 2012-08-17T12:04:00.643 回答