ios - 突出显示 uitableViewCell

标签 ios swift uitableview swift3

我想突出显示 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)
};

或者您可以直接在单元格类中设置选择颜色,例如

enter image description here

关于ios - 突出显示 uitableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541893/

相关文章:

ios - 将一个大的 swift 项目拆分为具有核心框架和模块框架的框架

swift - 组合多个 NSMutableDictionary

ios - 添加UIDatePicker后,打印错误

ios - 同一 TableView 中的单选和多选

ios - 您可以在付费的iOS应用程序中使用Google图表吗?

c# - 如何使用 C#.net 为 iPhone 中的推送通知准备后端

ios - 如果未设置变量,则等待设置变量

ios - 当我设置约束时,为什么我的 UITableView 将自身报告为 600x600,或者 Storyboard 中的内容?

ios - Swift Firebase QueryOrdered

.net - Objective-C to Monotouch (.NET) 单行代码转换