如何改变UITableViewCellselect视图的颜色?

如何更改单元格被选中时的灰色颜色?

在这里输入图像说明

当用户点击选定的行

(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0]; } 

selectedBackgroundView的颜色设置为自定义tableview单元格(它是UITableViewCell的子类)中的所需颜色:

 UIView * selectedBackgroundView = [[UIView alloc] initWithFrame:self.frame]; [selectedBackgroundView setBackgroundColor:[UIColor redColor]]; // set color here [self setSelectedBackgroundView:selectedBackgroundView]; [selectedBackgroundView release]; 

或者你可以在-tableView:cellForRowAtIndexPath:configuration它-tableView:cellForRowAtIndexPath: method:

 //... [cell setSelectedBackgroundView:selectedBackgroundView]; //... 

如何在委托方法中更改背景颜色:

 -(void)tableView:(UITableView *)tableView didSelectedRowAtIndexPath:(NSIndexPath *)indexPath 

谢谢@Kjuly和@ Selkie的回答。 我让它与你们两个一起工作。

  1. 首先设置selectedBackgroundView的backgroundColor。
  2. didSelectedRowAtIndexPath处更改cell.textLabel.backgroundColorcell.contentView.backgroundColor

再次感谢你。

尝试这个 :

 cell.selectionStyle = UITableViewCellSelectionStyleBlue;