0

その各セルにサブビューとして追加されたaがありUITableViewます。UIImage画像はPNG透明です。問題は、をスクロールするUITableViewと、画像が重なって、メモリの警告などが表示されることです。

セルを構成するための現在のコードは次のとおりです。

- (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:CellIdentifier];
    }

    int cellNumber = indexPath.row + 1;
    NSString *cellImage1 = [NSString stringWithFormat:@"c%i.png", cellNumber];
    UIImage *theImage = [UIImage imageNamed:cellImage1];
    UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
    [cell.contentView addSubview:cellImage];

    return cell;
}

UIImageサブビューを削除するためのコードは次のようになります。

[cell.imageView removeFromSuperview];

でもどこに置けばいいのかわからない。すべての行の間に配置しました。ifステートメントにelseを追加しました。うまくいかなかったようです!

4

2 に答える 2

1

UITableViewCellすでに画像ビューが添付されています。削除する:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];

追加:

[cell.imageview setImage:theImage];

そして、あなたは行ってもいいです!

結果コードは次のようになります。

- (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:CellIdentifier];
    }

    UIImage *cellImage = [UIImage imageNamed:[NSString stringWithFormat:@"c%i.png", indexPath.row + 1]];
    [cell.imageView setImage:cellImage];

    return cell;
}
于 2012-07-06T09:06:16.483 に答える
1

あなたはこれをケップします:

 [cell.imageview setImage:theImage];

削除します:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:theImage];
[cell.contentView addSubview:cellImage];
于 2012-07-06T09:15:07.920 に答える