1

以下に示すメソッドを使用した UITableView がありますcellForRowAtIndexPath。問題は、cellImage完全に上にスクロールして (行が表示されない)、解放した後にのみ表示されることです。奇妙なことは、別のテーブルビューの別のクラスに同一のコードがあり、画像が正常に表示されることです。何か案は?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    [cell.textLabel setText:[items objectAtIndex:indexPath.row]];
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundView:[[UIView alloc] init]];
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
    [cell addSubview:cellImage];
    [cellImage setFrame:CGRectMake(0, 0, 26, 24)];
    [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];

    return cell;
}
4

4 に答える 4

1
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];

行はメソッドに属していません。すべてのセルをスクロールするたびにメソッドを呼び出します。テーブルビューを作成した後、それらを viewDidLoad メソッドに入れます。

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
[cell addSubview:cellImage];

これらの行は非常に悪いものであり、メモリ リークを引き起こす可能性があります。

テーブルビューをスクロールするたびに、セルにイメージビューを追加します。テーブル ビューはセルを再利用します。これは、セルが再利用されるときに、新しいイメージビューをセルに追加することを意味します。さらにスクロールすると、コードのどこかでそれらを削除しない限り、すべてのセルで x 回の imageview が表示されます。

他のポイントは、セルにサブビューを追加する場合(たとえば、セルのinitメソッドで)、次のようにセルのcontenviewに追加することです

[cell.contentView addSubview:someView];

そうしないと、サブビューがアクセサリ ビューと重なる可能性があります。また、セルのサイズが変更されたときに問題が発生する可能性があります。

次のリンクからガイドを読むことをお勧めします 。 CH7-SW1

于 2012-08-09T13:14:49.037 に答える
0

多くのコメントに示されているように、いくつかの問題があります。

cellForRowAtIndexPathではなく、viewDidLoadメソッド(またはXIB経由)でビューとtableViewの背景を設定します

cell.imageViewプロパティを使用して左アイコンを設定するか、または[cell.contentView addSubview:cellImage]を使用してカスタムセルを作成します。後者を行う場合は、UILabelと同じ方法でtextLabelも追加する必要があります。

テーブルビューセルの背景を設定するには、次のデリゲートメソッドを使用します。

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
}

ただし、効率を上げるには、このUIColorをビューコントローラで一度インスタンス化し、[cell setBackgroundColor:self.cellBackground]として設定する必要があります。

于 2012-08-09T12:38:08.143 に答える
0

あなたが直面している問題は、セルのreuseIdentifierが原因です。また、cellForRowAtIndexPathが実行されるたびにImageviewを作成しています。

ここで2つの解決策を提案します。

最初のオプション:-このコードを置き換えるだけです:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

これは、動的な画像を扱う場合には適切な方法ではありません。つまり、サーバーからサムネイルをダウンロードしていて、データコンテンツが多いことを意味します。小さくて静的なデータであれば、問題ありません。

2番目のオプション:-このフォーマットされたコードを使用するだけで、タグ付きの現在のセルのオブジェクトを使用するたびにimageviewが作成されるわけではありません。

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


    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 26, 24)];  
        cellImage.tag = 88;
        [cell addSubview:cellImage];
        [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];

    }
    [cell.textLabel setText:[items objectAtIndex:indexPath.row]];
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundView:[[UIView alloc] init]];
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    UIImageView *cellImage = (UIImageView *)[cell viewWithtag:88];
    cellImage.image = [UIImage imageNamed:@"next@2x.png"];

}
于 2012-08-09T12:36:49.873 に答える
0

次のコードを使用して修正しました。

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
[cell setAccessoryView:cellImage];
于 2012-08-14T14:51:09.230 に答える