1

Uitableview の各 tableviewcell に各 webview を表示する必要があります。以下のコードを使用すると、2 つの要素がある場合、最初のセルは空ですが、2 番目のセルは正しいです。

hrs および hrsHtml にはすべての値が含まれています。問題は、テーブルビューの適切なセルに最後のデータのみが表示されていることです。他のセルは空白です。

また、セルの合計は 2 です。最初は 2 番目のセルしか表示できませんでしたが、スクロールするとテーブルビューがリロードされ、2 番目のセルが消えて 1 番目のセルが表示されます。

- (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 のみ

ここに画像の説明を入力

テーブルビューがスクロールするときのスクリーンショット、セル 1 の Web ビューのみ、セル 2 が消える

ここに画像の説明を入力

4

3 に答える 3

1

hrsとは単一のオブジェクトであるため、hrsHtml複数のセルがあってもそれぞれ 1 つしかありません。を変更するhrsと、セルを共有しているように見えるため、すべてのセルで変更されます。(これらの変数が指すオブジェクトを変更する他のコードがどこかにある場合を除きます。)

もう 1 つの奇妙な点は、配列を使用して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;

}

Now the webview loads correctly in every 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 に答える