2013-05-08 79 views
-1

我希望整个单元格以蓝色打印,但它只显示一个小带。如何在TableView上设置单元格的颜色?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *cellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
} 
[cell.contentView setBackgroundColor:[UIColor blueColor]];  
cell.textLabel.text = [NSString stringWithFormat:@"%@", [cat objectAtIndex:indexPath.row]];  
return cell;} 

我的屏幕捕获是这样的:

enter image description here

+0

试试这个http://stackoverflow.com/questions/6346721/uitableviewcell-background-color-problem – 2013-05-08 11:59:37

回答

2

添加此行之后[cell.contentView setBackgroundColor:的UIColor blueColor]];行:

cell.textLabel.backgroundColor = [UIColor clearColor]; 
1

,因为这样UITableView更改单元格的背景颜色,你必须实现tableView:willDisplayCell:forRowAtIndexPath:并设置单元格的背景存在。你应该设置整个单元格的背景,而不仅仅是其contentView,否则附件视图将不会突出显示。

- (void)tableView:(UITableView *)tableView 
    willDisplayCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // ... 
    cell.backgroundColor = ...; 
} 

您可能还需要使单元格中的各种子视图具有透明背景颜色。

cell.titleLabel.backgroundColor = [UIColor clearColor]; 
cell.titleLabel.opaque = NO; 
1

使用此

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *cellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; 
} 
[cell.contentView setBackgroundColor:[UIColor blueColor]];  
cell.textLabel.backgroundColor = [UIColor clearColor]; 
cell.textLabel.text = [NSString stringWithFormat:@"%@", [cat objectAtIndex:indexPath.row]];  
return cell;} 
相关问题