UITableViewCell多选时选择的背景色 [英] UITableViewCell Selected Background Color on Multiple Selection

查看:142
本文介绍了UITableViewCell多选时选择的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// Doesn't work
cell.selectionStyle = .Blue
//Works when the selection is not multiple, if it's multiple with each selection the previous one disappear...
let cellBGView = UIView()
cellBGView.backgroundColor = UIColor(red: 0, green: 0, blue: 200, alpha: 0.4)
cell.selectedBackgroundView = cellBGView

任何答案如何设置所选单元格的背景颜色?

Any answer how to set background color of the cells which are selected?

推荐答案

Swift 4.2

对于多项选择,您需要将UITableView属性allowsMultipleSelection设置为 true .

For multiple selections you need to set the UITableView property allowsMultipleSelection to true.

myTableView.allowsMultipleSelection = true

myTableView.allowsMultipleSelection = true

如果您将 UITableViewCell 子类化,则可以在自定义单元格类中覆盖setSelected(_ selected: Bool, animated: Bool)方法.

In case you subclassed the UITableViewCell, you override setSelected(_ selected: Bool, animated: Bool) method in your custom cell class.

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

     if selected {
         contentView.backgroundColor = UIColor.green
     } else {
         contentView.backgroundColor = UIColor.blue
     }
 }

这篇关于UITableViewCell多选时选择的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆