ios - swift - 在 UITableVIew 中将多选设置为 true 时如何设置选择颜色

标签 ios swift

如果将多选设置为真,颜色将更改为您为触摸的每个单元格指定的颜色。

但我想要的是将多选设置为 true 并仅将触摸单元格的颜色更改为白色。

这有可能吗?

这是我正在申请的来源。

class CustomCell: UITableViewCell {

@IBOutlet weak var btnCheck: UIButton!

override func layoutSubviews() {
    super.layoutSubviews()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    if selected {
        contentView.backgroundColor = UIColor.blue
        btnCheck.isSelected = true
        self.isUserInteractionEnabled = false
    } else {
        contentView.backgroundColor = UIColor.white
        btnCheck.isSelected = false
    }
}

最佳答案

您可以使用以下代码执行此操作: 对我有用

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard  let cell : KichenListTableViewCell = tableView.cellForRow(at: indexPath) as? KichenListTableViewCell else {
            return
        }
        cell.backgroundColor = UIColor.red
    }

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    guard  let cell  : KichenListTableViewCell = tableView.cellForRow(at: indexPath) as? KichenListTableViewCell else {
        return
    }
    cell.backgroundColor = UIColor.white
}

希望对你有帮助,谢谢

关于ios - swift - 在 UITableVIew 中将多选设置为 true 时如何设置选择颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51418306/

相关文章:

ios - Firebase 和 Swift 3 代码不再执行

ios - 快速堆栈 View 中的表格 View

ios - 是否可以在应用程序运行时在设备上训练 CoreML 模型?

swift - 无法区分 ARKit 检测到的平面和使用 HitTest 放置的数字对象

ios - Swift:如何将 Action 链接到 tableViewCell 内的 View ?

javascript - 在为发布进行编译时,如何让React Native将JS bundle 放入Share Extension中?

ios - 如何在 iOS 快速模块化应用程序中执行路由?

ios - 资源不会在 iOS8 上加载,但会在 iOS9 上加载

ios - Metal IOS 中的网格

ios - '初始化(contentsOfURL :)' is unavailable: APIs deprecated as of iOS 7 and earlier are unavailable in Swift