Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-10-2010, 06:32 PM   #1 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default 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.
iSDK is offline   Reply With Quote
Old 09-10-2010, 06:39 PM   #2 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

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?
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-10-2010, 06:50 PM   #3 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Surely there is an easier way than typing it in for every cell.

[[cell detailTextLabel] setText:@"Example"];
}[/code]
Quote:
Originally Posted by kelvinkao View Post
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?
iSDK is offline   Reply With Quote
Old 09-10-2010, 07:07 PM   #4 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

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.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-10-2010, 07:08 PM   #5 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Would you mind giving some example code?
Quote:
Originally Posted by kelvinkao View Post
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.
iSDK is offline   Reply With Quote
Old 09-10-2010, 10:39 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 14
lynngobin is on a distinguished road
Default

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;
}
}
lynngobin is offline   Reply With Quote
Old 09-11-2010, 07:38 AM   #7 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

thanks!
Quote:
Originally Posted by lynngobin View Post
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;
}
}
iSDK is offline   Reply With Quote
Old 09-11-2010, 09:24 AM   #8 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

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 View Post
thanks!
iSDK is offline   Reply With Quote
Old 09-11-2010, 02:08 PM   #9 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Post your code.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-11-2010, 02:15 PM   #10 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

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 View Post
Post your code.
iSDK is offline   Reply With Quote
Old 09-11-2010, 03:10 PM   #11 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

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.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-11-2010, 03:19 PM   #12 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

The other color set is white! Its in the method cellforrowatindex path.
Quote:
Originally Posted by kelvinkao View Post
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.
iSDK is offline   Reply With Quote
Old 09-11-2010, 03:30 PM   #13 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Quote:
Originally Posted by iSDK View Post
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.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-11-2010, 03:57 PM   #14 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

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 View Post
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.
iSDK is offline   Reply With Quote
Old 09-11-2010, 04:11 PM   #15 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

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.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 09-11-2010, 04:13 PM   #16 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

How can i set the color to white?

Quote:
Originally Posted by kelvinkao View Post
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.
iSDK is offline   Reply With Quote
Old 09-11-2010, 04:24 PM   #17 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

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 View Post
How can i set the color to white?
iSDK is offline   Reply With Quote
Old 09-11-2010, 06:23 PM   #18 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

What do you think this does?

Code:
cell.backgroundColor = altCellColor, altCellColor2;
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-11-2010, 07:35 PM   #19 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

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 View Post
What do you think this does?

Code:
cell.backgroundColor = altCellColor, altCellColor2;
iSDK is offline   Reply With Quote
Old 09-11-2010, 07:37 PM   #20 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

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.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 09-11-2010, 07:38 PM   #21 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Ok, thanks
Quote:
Originally Posted by BrianSlick View Post
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.
iSDK is offline   Reply With Quote
Old 09-12-2010, 12:52 AM   #22 (permalink)
Registered Member
 
kelvinkao's Avatar
 
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
kelvinkao is on a distinguished road
Send a message via AIM to kelvinkao
Default

Quote:
Originally Posted by iSDK View Post
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.
__________________
My dev blog:
http://www.kelvinkaodev.com
kelvinkao is offline   Reply With Quote
Old 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
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by kelvinkao View Post
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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 09-12-2010, 07:47 AM   #24 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

Soz i forgot to take them out. It havent declared both of them in my full code.
Quote:
Originally Posted by Kalimba View Post
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.
iSDK is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» 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
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:55 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0