简体   繁体   English

UItableView:无法更改单元格的颜色

[英]UItableView: unable to change color of a cell

I am new to iPhone programming and I was unable to change background of my Table view cell 我是iPhone编程的新手,我无法更改表格视图单元格的背景

- (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;

} }

Please help me to understand the reason why?? 请帮我理解为什么??

Thanx in advance Thanx提前

Ok got the answer 好的,得到了​​答案

 - (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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM