我想突出显示我的tableView中的一些单元格。通过突出显示,我的意思是将单元格的背景颜色设置为蓝色,例如复制文本时使用的蓝色。我用这个代码来检查它是否工作。
public func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.setHighlighted(true, animated: false)
}我也试过这个
cell.setSelected(true, animated: false)在这两种情况下,细胞都变成灰色而不是蓝色。有什么解决方案吗?
发布于 2017-04-21 19:48:37
更改单元格的selectionStyle属性。如果您想将其更改为
UITableViewCellSelectionStyleBlue,它将是蓝色。对于备用方法,您可以获取另一个SO answer
例如,用于
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
cell.selectionStyle = .blue
}UITableViewCell有三种默认的选择样式:
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray,
UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
};或者,您可以直接在单元格类中设置Selection color,如下所示

发布于 2017-04-21 19:50:22
试着这样做
public func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.selectionStyle = .blue
}https://stackoverflow.com/questions/43541893
复制相似问题