我想突出显示 tableView 中的一些单元格。通过突出显示,我的意思是将单元格的背景颜色设置为复制文本时使用的蓝色。我尝试过这段代码来检查它是否有效
public func tableView(_ tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.setHighlighted(true, animated: false)
}
我也尝试过这个
cell.setSelected(true, animated: false)
在这两种情况下,单元格都会变成灰色而不是蓝色。有解决办法吗?
最佳答案
Change the selectionStyle property of yourcell. If you want to change it to
UITableViewCellSelectionStyleBlue
, it will be blue. for alternate method , you can get the another 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)
};
或者您可以直接在单元格类中设置选择颜色
,例如
关于ios - 突出显示 uitableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541893/