3

actually i have found some thread that asking question like mine. but every solution that given in those thread can not be use in my code.. the last link i was read is How to customize the background color of a UITableViewCell?

my problem is i just want to change my tableviewcell background color with my own color. can somebody help me??

Community
  • 1
  • 1
R. Dewi
  • 4,141
  • 9
  • 41
  • 66
  • Show the code that you tried in willDisplayCell:forRowAtIndexPath: (the whole method including the method name and parameters). Are you using UITableViewCell or a custom subclass? –  Nov 24 '10 at 04:06
  • -(void)tableView:(UITableView *)tableView wilLDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ UIView* backgroundView = [ [ [ UIView alloc ] initWithFrame:CGRectZero ] autorelease ]; backgroundView.backgroundColor = [ UIColor yellowColor ]; cell.backgroundView = backgroundView; for ( UIView* view in cell.contentView.subviews ) { view.backgroundColor = [ UIColor clearColor ]; } //cell.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.5 alpha:1]; } – R. Dewi Nov 24 '10 at 04:50
  • there is a spelling mistake in your code is should be *willDisplayCell* but in the code you posted it is *wilLDisplayCell* please check that ... – Gyani Nov 24 '10 at 05:17
  • thx gyani....i dont realized it..thank you... :) – R. Dewi Nov 24 '10 at 06:15

5 Answers5

3

How about something like:

[myCell setBackgroundColor:[UIColor colorWithRed:0.2 blue:0.5 green:0.2 alpha:1]];

One place you could put this would be your table view's willDisplayCell:forRowAtIndexPath: data source method. Or else you could subclass UITableViewCell and set it in your overridden initializer. Or you could load the cell from a XIB and use Interface Builder to set the color.

HotFudgeSunday
  • 1,403
  • 2
  • 23
  • 29
alexantd
  • 3,543
  • 3
  • 27
  • 41
  • 1
    This has to be done in willDisplayCell:forRowAtIndexPath:. It will not work in cellForRowAtIndexPath. See the docs for UITableViewCell. –  Nov 24 '10 at 03:54
  • i was try like that, sorry not declare it in my question, but there's still not displayed the background color – R. Dewi Nov 24 '10 at 03:56
  • it's not displayed too, even i moved it into willDisplayCell:forRowAtIndexPath – R. Dewi Nov 24 '10 at 03:59
2

try this..

cell.backGroundColor = [UIColor colorWithRed:190.0/255.0 blue:190.0/255.0 green:190.0/255.0 alpha:1.0];

Write this code in cellForRowAtIndexPath function. U will get these kind of different RGB combinations simply from MS Paint and alpha is the opacity ranging in between 0.0 to 1.0.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
iOSiOS
  • 214
  • 2
  • 5
  • 10
1

Before iOS 7 Simply add this method

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

       [cell setBackgroundColor:[UIColor colorWithRed:(221.0/255.0) green:(221.0/255.0) blue:(221.0/255.0) alpha:1.0]];

}

Now in iOS 7 if you put this code in cellForRowAtIndexPath it will work

[cell setBackgroundColor:[UIColor colorWithRed:(221.0/255.0) green:(221.0/255.0) blue:(221.0/255.0) alpha:1.0]];
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
Nagendra Tripathi
  • 923
  • 12
  • 23
1

another link that help this problem... UITableViewCell color issues with custom table view background

Community
  • 1
  • 1
R. Dewi
  • 4,141
  • 9
  • 41
  • 66
1

finnaly i found out how to solve this problem. but i was choose to use an image. I was declare my way to solve it in my another question in : is there somebody who made an application using background for tableview and tableviewcell?

Community
  • 1
  • 1
R. Dewi
  • 4,141
  • 9
  • 41
  • 66