UITableView单元格选择颜色?

我创建了一个自定义UITableViewCell。表格视图显示的数据很好。我遇到的问题是,当用户触摸表格视图的单元格时,我想显示该单元格的背景色,而不是默认的[蓝色]值,以突出显示该单元格的选择。我使用此代码,但没有任何反应:


cell.selectedBackgroundView.backgroundColor=[UIColor blackColor];


守着一只汪
浏览 424回答 3
3回答

猛跑小猪

我认为您走在正确的道路上,但是根据以下课程的类定义selectedBackgroundView:对于普通样式表(UITableViewStylePlain)中的单元,默认值为nil;对于节组表UITableViewStyleGrouped,默认值为nil。因此,如果您使用的是普通样式表,则需要UIView分配一个具有所需背景色的新值,然后将其分配给selectedBackgroundView。或者,您可以使用:cell.selectionStyle = UITableViewCellSelectionStyleGray;如果选中该单元格时您想要的只是一个灰色背景。希望这可以帮助。

米脂

无需自定义单元格。如果只想更改单元格的选定颜色,则可以执行以下操作:目标C:UIView *bgColorView = [[UIView alloc] init];bgColorView.backgroundColor = [UIColor redColor];[cell setSelectedBackgroundView:bgColorView];迅速:let bgColorView = UIView()bgColorView.backgroundColor = UIColor.redColor()cell.selectedBackgroundView = bgColorView斯威夫特3:let bgColorView = UIView()bgColorView.backgroundColor = UIColor.redcell.selectedBackgroundView = bgColorViewSwift 4.x:let bgColorView = UIView()bgColorView.backgroundColor = .redcell.selectedBackgroundView = bgColorView

HUWWW

如果您有一个分组的表,每个节只有一个单元格,只需在代码中添加以下额外的行即可: bgColorView.layer.cornerRadius = 10;UIView *bgColorView = [[UIView alloc] init];[bgColorView setBackgroundColor:[UIColor redColor]];bgColorView.layer.cornerRadius = 10;[cell setSelectedBackgroundView:bgColorView];[bgColorView release]; 不要忘记导入QuartzCore。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

iOS