2011-03-30 71 views
1

我是新iPhone编程,我无法改变我的表视图单元格的背景的UITableView:无法更改单元格的颜色

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    if((indexPath.row%2)!=0) 
    { 
     [cell setBackgroundColor:[UIColor clearColor]]; 
     [cell setBackgroundColor:[UIColor redColor]]; 
     //[cell ]; 
    } 
    else 
    { 
     [cell setBackgroundColor:[UIColor whiteColor]]; 
    } 

} 

// Configure the cell... 


[cell setText:[menuItems objectAtIndex:indexPath.row]]; 

return cell; 

}

请帮助我了解为什么??

Thanx提前

回答

4

好得到的答案

- (void)tableView: (UITableView*)tableView willDisplayCell: (UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath 
{ 
    cell.backgroundColor = indexPath.row % 2 
    ? [UIColor colorWithRed: 0.0 green: 0.0 blue: 1.0 alpha: 1.0] 
    : [UIColor whiteColor]; 
    cell.textLabel.backgroundColor = [UIColor clearColor]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 
}