0

を使用してiPhoneアプリで作業していUITableViewます。私のテーブルビューには、複数のセルを持つ複数のセクションがあります。各セルのセルの背景色を指定したい。つまりeach cell should have different colors

例: 20 個のセルがある場合、20 個のセルごとに異なるセルの背景色が必要です。

各セルにセルの背景色/画像を与えることが可能かどうか教えてください。これを行うように私を案内してもらえますか?

前もって感謝します。

編集

私のTableviewcellの色は以下のようになります。

First Cell background color : white color
Second Cell background color : red color
Third Cell background color : orange color
Fourth cell background color : gray color
Fifth Cell background color : white color
Sixth cell background color : white color
Seventh cell background color : white color
Eight cell background color : gray color
Ninth cell background color : red color
Tenth cell background color : red color

このように、テーブルビューのセルの背景色を指定したいと思います。UITableView をリロードするか、新しく開いたときに背景色が変更されます。このようにするのを手伝ってもらえますか?iPhoneアプリのテーブルビューでできますか?

前もって感謝します。

4

3 に答える 3

0

少し複雑ですが、cellForRowAtIndexPathで行う方法は次のとおりです。このコードをメソッドに貼り付けるだけです。上記の仕様通りに書きました。

if (indexPath.row == 0 || indexPath.row == 4 || indexPath.row == 5 || indexPath.row == 6) {
            [cell.textLabel setBackgroundColor:[UIColor whiteColor]];
            cell.contentView.backgroundColor = [UIColor whiteColor];
            [cell.textLabel setTextColor:[UIColor blackColor]];
        } else {
            if (indexPath.row == 1 || indexPath.row == 8 || indexPath.row == 9){
                 [cell.textLabel setBackgroundColor:[UIColor redColor]];
                 cell.contentView.backgroundColor = [UIColor redColor];
                 [cell.textLabel setTextColor:[UIColor whiteColor]];
            } else {
                  if (indexPath.row == 3 || indexPath.row == 7){
                 [cell.textLabel setBackgroundColor:[UIColor grayColor]];
                 cell.contentView.backgroundColor = [UIColor grayColor];
                 [cell.textLabel setTextColor:[UIColor whiteColor]];
                 } else {
                      [cell.textLabel setBackgroundColor:[UIColor orangeColor]];
                      cell.contentView.backgroundColor = [UIColor orangeColor];
                      [cell.textLabel setTextColor:[UIColor blackColor]];
}}}

もちろん、これを行う他の方法があります(たとえば、を使用switchするか、UIColorを宣言し、呼び出されているものに応じてその値を変更するなどindexPath.row)。ただし、実装後は、自分で簡略化できます。

均一性が必要な場合は、textLabelとcontentViewの両方の背景色を設定する必要があることに注意してください。

乾杯!

于 2012-05-07T16:22:10.130 に答える