2012-05-07 27 views
0

UITableViewを使用してiPhoneアプリで作業しています。私のテーブルビューでは、私は複数のセルを持つ複数のセクションがあります。私は各セルのセルの背景色を与えたい。それはeach cell should have different colorsを意味します。iPhoneアプリの各UITableViewCell背景色を変更しますか?

例:20個のセルがある場合は、20個のセルごとに異なるセルの背景色を使用する必要があります。

セルの背景色/画像を各セルに割り当てることができるかどうか教えてください。 これは私に指示してもらえますか?

ありがとうございます。

EDIT

マイ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アプリのテーブルビューで行うことは可能ですか?

ありがとうございます。

+0

http://stackoverflow.com/questions/281515/how-to-customize-the-background-color-of-a-uitableviewcellの可能な複製 –

答えて

0

これを行うには、実際には標準tableView:cellForRowAtIndexPath:ワークフローを使用できません。セルの背景色は、セルの選択された状態を管理しながらテーブルビューが変更するため、特殊なケースです。

代わりにthis answerに記載されているように、tableView:willDisplayCell:atIndexPath:メソッドを実装する必要があります。これをカバーする古いWWDCビデオがあります。私は2009年または2010年のセッションから考えています。

はたとえば、あなたはこのような何かを行うことができます:

- (void)tableView:(UITableView*)tableView 
    willDisplayCell:(UITableViewCell*)cell 
forRowAtIndexPath:(NSIndexPath*)indexPath 
{ 
    cell.backgroundColor = /* random color here */; 
} 
+0

実際、あなたは 'cellForRowAtIndexPath'でそれを行うことができます複雑な方法を示す回答を投稿します。 – AMayes

+0

申し訳ありませんが、これはアップルのエンジニアが少なくとも1つのWWDCビデオでカバーしてきた非常によく知られた制限です。 cellForRowAtIndexPathを使用すると信頼性が低くなり、奇妙な動作や不整合な結果が発生します。セルの背景色を設定する唯一の信頼できる正式にサポートされている方法は、willDisplayCellを使用することです。 –

0

は、それは少し複雑ですが、ここではcellForRowAtIndexPathでそれを行う方法です。この

- (UITableViewCell *)tableView:(UITableView *)tTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    double numSections = [self numberOfSectionsInTableView:tableView]; 
    double numRows = [self tableView:tableView numberOfRowsInSection:indexPath.section]; 
    [cell setBackgroundColor:[UIColor colorWithRed:0.8/numRows*((double)indexPath.row)+0.2 green:0.8/numSections*((double)indexPath.section)+0.2 blue:((double)(random()%1000))/1000.0 alpha:1]]; 
    return cell; 
} 
0

を試してみてください。このコードをメソッドに貼り付けるだけです。私は上記のあなたの仕様に従って書いた。

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]]; 
}}} 

(例えば宣言UIColorを有し、indexPath.rowが呼び出さされているに応じてその値を変更することにより、switch、またはを使用するなど)これを行うために、当然のことながら、他の方法があります。しかし、実装後、あなたはそれを自分で単純化することができます。

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

乾杯!

+1

ランダムな色付けが必要な場合を除きます。それで、これは完全に間違った方法です。 – AMayes

関連する問題