How do I create rows with alternating colors for a UITableView on iPhone?
Posted
by Mat
on Stack Overflow
See other posts from Stack Overflow
or by Mat
Published on 2010-04-02T21:33:03Z
Indexed on
2010/04/02
21:43 UTC
Read the original article
Hit count: 254
Hi all, i would to have alternate 2 colors of rows, like the first black, the second white, the third black, etc, etc...
my approach is like a basic exercise of programming to calculate if a number is odd number or not:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
cell = ((MainCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]);
if (cell==nil) {
NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:self options:nil];
for (id currentObject in topLevelObjects){
if ([currentObject isKindOfClass:[UITableViewCell class]]){
if ((indexPath.row % 2)==0) {
[cell.contentView setBackgroundColor:[UIColor purpleColor]];
}else{
[cell.contentView setBackgroundColor:[UIColor whiteColor]];
}
cell = (MainCell *) currentObject;
break;
}
}
}else {
AsyncImageView* oldImage = (AsyncImageView*)
[cell.contentView viewWithTag:999];
[oldImage removeFromSuperview];
}return cell;
The problem is that when i do a rapid scroll, the background of cells become like the last 2 cell black, the first 2 cell white or something like this, but if i scroll slow works fine. I think the problem is the cache of reusableCell.
Any ideas?
TIA
© Stack Overflow or respective owner