Created
January 2, 2013 22:21
-
-
Save keighl/4438822 to your computer and use it in GitHub Desktop.
Alternating UITableViewCell Background Colors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UITableViewCell *)tableView:(UITableView *)tableView | |
cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *CellIdentifier = @"VenueCell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (!cell) | |
{ | |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle | |
reuseIdentifier:CellIdentifier]; | |
cell.backgroundView = [[UIView alloc] init]; | |
} | |
UIColor *evenColor = [UIColor lightGrayColor]; | |
UIColor *oddColor = [UIColor lightGrayColor]; | |
cell.backgroundView.backgroundColor = (indexPath.row % 2) ? evenColor : oddColor; | |
cell.textLabel.backgroundColor = (indexPath.row % 2) ? evenColor : oddColor; | |
cell.detailTextLabel.backgroundColor = (indexPath.row % 2) ? evenColor : oddColor; | |
Venue *venue = (Venue *)[self.venues objectAtIndex:indexPath.row]; | |
cell.textLabel.text = venue.name; | |
if (venue.category != nil) | |
cell.detailTextLabel.text = venue.category; | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Keighl,
A couple of comments on your code.
Update:
[Ah, I just realized you are probably assuming the first row in the table (for the user anyway) is row 1, not row 0, hence why you set up your even/odd the way you did. Sorry about that!]
Again, you probably noticed this but I wanted to make sure. Thanks for posting.
Norm