如何更改UITableViewCell选择视图的颜色?

时间:2012-07-20 03:28:47

标签: iphone ios uitableview

如何在选择单元格时更改灰色?

enter image description here

5 个答案:

答案 0 :(得分:3)

当用户点击选定行

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

答案 1 :(得分:2)

在自定义tableview单元格(UITableViewCell的子类)中将selectedBackgroundView的颜色设置为您想要的颜色:

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

或者您可以使用-tableView:cellForRowAtIndexPath:方法配置它:

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

  1. 首先设置selectedBackgroundView的backgroundColor。
  2. cell.textLabel.backgroundColor更改cell.contentView.backgroundColordidSelectedRowAtIndexPath
  3. 再次感谢你。

答案 4 :(得分:1)

试试这个:

cell.selectionStyle = UITableViewCellSelectionStyleBlue;
相关问题