0

たとえば、テーブル ビューの最初のセルの背景色を青に設定しようとしていますが、うまくいきません。スクロールを開始するたびに、青色が別のセルにジャンプします。私はこれらのコードを試しました。

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{

if([[self.menu objectAtIndex:indexPath.row] isEqual:@"a string"]){


    cell.contentView.backgroundColor=[UIColor blueColor] ;



}

}

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell       forRowAtIndexPath:(NSIndexPath *)indexPath
 {

if(indexPath.row==0){


    cell.contentView.backgroundColor=[UIColor blueColor] ;



}

}

そして、インデックスパスのcellfro行でも試しました。何か足りない?

これは私が cellforrowatindexpath に使用しているコードです:

   if(indexPath.row==0){


    cell.contentView.backgroundColor=[UIColor blueColor] ;



}
4

2 に答える 2

0

if に else も追加してください...

if(indexPath.row==0){


        cell.contentView.backgroundColor=[UIColor blueColor] ;



    }
else
 cell.contentView.backgroundColor=[UIColor whiteColor] ;

uitableviewCell は再利用されています...

于 2013-04-03T22:18:00.860 に答える
-1

あなたの場合、メモリを節約するためにスクロールするときにテーブルビューのセルが再利用されるため、セルの色がジャンプします。奇妙なことが起こらないようにするには、セルの色を具体的に設定する必要があります。

最初のビットが完了しました。つまり、 indexPath.row == 0 のときにセルに色を付けて、2 番目の部分をカバーします。

必要なのは、次のようなパターンに従うことです。

if(indexPath.row == 0){
    //if its the first cell then keep it this color
    cell.contentView.backgroundColor = [UIColor blueColor];
}
else {
    //Any other cell keep it this color
    cell.contentView.backgroundColor = [UIColor whiteColor];
}
于 2013-04-03T22:24:11.903 に答える