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

当用户单击选定行时
(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];
}
在自定义表格视图单元格(UITableViewCell 的子类)中将 's 颜色设置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];
//...
如何在委托方法中更改背景颜色:
-(void)tableView:(UITableView *)tableView didSelectedRowAtIndexPath:(NSIndexPath *)indexPath
感谢@Kjuly 和@Selkie 的回答。我让它对你们俩都有效。
selectedBackgroundView的背景颜色。cell.textLabel.backgroundColor和cell.contentView.backgroundColor在didSelectedRowAtIndexPath。再次感谢你。
试试这个 :
cell.selectionStyle = UITableViewCellSelectionStyleBlue;