09-10-2010, 06:32 PM
#1 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Alternating TableView Cell Colors
Hi, I have completed a grouped tableview project, and i am just stuck on alternating cell colors.
I would like to have the first cell blue and then the second cell white. And when the second cell is white, i would like the background of the cell to be black. This would then carry on throughout the tableview.
Thanks in advance.
09-10-2010, 06:39 PM
#2 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
In your data source class, when you are create the cells for the table view, you can just change what you returned according to the row number, right?
09-10-2010, 06:50 PM
#3 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Surely there is an easier way than typing it in for every cell.
[[cell detailTextLabel] setText:@"Example"];
}[/code]
Quote:
Originally Posted by
kelvinkao
In your data source class, when you are create the cells for the table view, you can just change what you returned according to the row number, right?
09-10-2010, 07:07 PM
#4 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
You don't need to type it in for every cell, per se. Just do calculations based on the row number, figure out if it's odd or even, and color accordingly.
09-10-2010, 07:08 PM
#5 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Would you mind giving some example code?
Quote:
Originally Posted by
kelvinkao
You don't need to type it in for every cell, per se. Just do calculations based on the row number, figure out if it's odd or even, and color accordingly.
09-10-2010, 10:39 PM
#6 (permalink )
Registered Member
Join Date: Aug 2010
Posts: 14
Try some thing like this:
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row == 0 || indexPath.row%2 == 0) {
UIColor *altCellColor = [UIColor colorWithRed:256/256.0 green:237/256.0 blue:227/256.0 alpha:1.0];
cell.backgroundColor = altCellColor;
}
}
09-11-2010, 07:38 AM
#7 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
thanks!
Quote:
Originally Posted by
lynngobin
Try some thing like this:
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row == 0 || indexPath.row%2 == 0) {
UIColor *altCellColor = [UIColor colorWithRed:256/256.0 green:237/256.0 blue:227/256.0 alpha:1.0];
cell.backgroundColor = altCellColor;
}
}
09-11-2010, 09:24 AM
#8 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
I have just implemented it into my application, and all the cells are red/white but in no particular order. Also if i scroll up and down all of the cells change to red.
Quote:
Originally Posted by
iSDK
thanks!
09-11-2010, 02:08 PM
#9 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
Post your code.
09-11-2010, 02:15 PM
#10 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Code:
if (indexPath.row%2 == 0)
{
UIColor *altCellColor = [UIColor colorWithRed:256/256.0 green:237/256.0 blue:227/256.0 alpha:1.0];
cell.backgroundColor = altCellColor;
}
Quote:
Originally Posted by
kelvinkao
Post your code.
09-11-2010, 03:10 PM
#11 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
What method is this in? Where is the other color set?
My guess is that you are not setting the odd and even numbers equal number of times.
09-11-2010, 03:19 PM
#12 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
The other color set is white! Its in the method cellforrowatindex path.
Quote:
Originally Posted by
kelvinkao
What method is this in? Where is the other color set?
My guess is that you are not setting the odd and even numbers equal number of times.
09-11-2010, 03:30 PM
#13 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
Quote:
Originally Posted by
iSDK
The other color set is white! Its in the method cellforrowatindex path.
My guess is that you are not setting colors correctly when you re-use the cells. You can either set BOTH colors in that cellWillDisplay method, or post the cellForRowAtIndexPath method for us to have a look.
09-11-2010, 03:57 PM
#14 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:SimpleTableIdentifier] autorelease];
/*cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier] autorelease];
*/
}
if (indexPath.row%3 == 0)
{
UIColor *altCellColor = [UIColor colorWithRed:256/256.0 green:237/256.0 blue:227/256.0 alpha:1.0];
cell.backgroundColor = altCellColor;
}
Quote:
Originally Posted by
kelvinkao
My guess is that you are not setting colors correctly when you re-use the cells. You can either set BOTH colors in that cellWillDisplay method, or post the cellForRowAtIndexPath method for us to have a look.
09-11-2010, 04:11 PM
#15 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
It shouldn't say %3. This is why your colors appear random. Take that block of code out and set both colors in cellWillDisplay
Or instead you can set ALL cells to white in that method. And then in cellWillDisplay, use the %2 logic to set half of them to red. You can also set both colors in the cellForRowAtIndexPath method. As long as you don't do that %3 calculation (which changes cells 0, 3, 6, 9, etc.), it should be fine.
09-11-2010, 04:13 PM
#16 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
How can i set the color to white?
Quote:
Originally Posted by
kelvinkao
It shouldn't say %3. This is why your colors appear random. Take that block of code out and set both colors in cellWillDisplay
Or instead you can set ALL cells to white in that method. And then in cellWillDisplay, use the %2 logic to set half of them to red. You can also set both colors in the cellForRowAtIndexPath method. As long as you don't do that %3 calculation (which changes cells 0, 3, 6, 9, etc.), it should be fine.
09-11-2010, 04:24 PM
#17 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
I have tried
Code:
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row%2 == 0) {
UIColor *altCellColor = [UIColor colorWithRed:200/256.0 green:237/256.0 blue:255/256.0 alpha:1.0];
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
cell.backgroundColor = altCellColor, altCellColor2;
}
}
But when i scroll all of the cells turn blue, but they all start off as blue and white.
Quote:
Originally Posted by
iSDK
How can i set the color to white?
09-11-2010, 06:23 PM
#18 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
What do you think this does?
Code:
cell.backgroundColor = altCellColor, altCellColor2;
09-11-2010, 07:35 PM
#19 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Hehe i know! I managed to get it to work by:
Code:
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIColor *altCellColor = [UIColor colorWithRed:200/256.0 green:237/256.0 blue:255/256.0 alpha:1.0];
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
cell.backgroundColor = altCellColor;
}
if (indexPath.row == 1) {
UIColor *altCellColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
cell.backgroundColor = altCellColor2;
}
}
Quote:
Originally Posted by
BrianSlick
What do you think this does?
Code:
cell.backgroundColor = altCellColor, altCellColor2;
09-11-2010, 07:37 PM
#20 (permalink )
Emphasizing Fundamentals
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
Uh, something isn't right there.
For your colors, be sure to use float values. 1.0, etc. For the white one, just use whiteColor.
09-11-2010, 07:38 PM
#21 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Ok, thanks
Quote:
Originally Posted by
BrianSlick
Uh, something isn't right there.
For your colors, be sure to use float values. 1.0, etc. For the white one, just use whiteColor.
09-12-2010, 12:52 AM
#22 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
Quote:
Originally Posted by
iSDK
Hehe i know! I managed to get it to work by:
Code:
- (void)tableView: (UITableView *)tableView willDisplayCell: (UITableViewCell *)cell forRowAtIndexPath: (NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UIColor *altCellColor = [UIColor colorWithRed:200/256.0 green:237/256.0 blue:255/256.0 alpha:1.0];
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
cell.backgroundColor = altCellColor;
}
if (indexPath.row == 1) {
UIColor *altCellColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
UIColor *altCellColor2 = [UIColor colorWithRed:1 green:1 blue:1 alpha:1.0];
cell.backgroundColor = altCellColor2;
}
}
By the way, you are declaring altCellColor and altCellColor2 twice (and only use it once for each). You probably got compiler warnings for those.
09-12-2010, 03:04 AM
#23 (permalink )
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Quote:
Originally Posted by
kelvinkao
By the way, you are declaring altCellColor and altCellColor2 twice (and only use it once for each). You probably got compiler warnings for those.
Technically, those are two different declarations for each variable (each set is contained within its own scope), but you should indeed be getting some "declared but unused variable" warnings.
09-12-2010, 07:47 AM
#24 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Soz i forgot to take them out. It havent declared both of them in my full code.
Quote:
Originally Posted by
Kalimba
Technically, those are two different declarations for each variable (each set is contained within its own scope), but you should indeed be getting some "declared but unused variable" warnings.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 341
13 members and 328 guests
bignoggins , carlandrews , cgokey , flamingliquid , givensur , hzwegjxg , ilmman , jenniead38 , linkmx , mraalex , PixelInteractive , Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38