ios - UITableViewCell 3 Time Selected 在多选时更改背景颜色
问题描述
我有 5 行。选择 3 个或更多时,背景颜色会发生变化。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
let backgroundView = UIView()
backgroundView.backgroundColor = YOUR_COLOR_HERE
cell.selectedBackgroundView = backgroundView
return cell
}
这些代码改变了免费的颜色。但是我希望在选择 3 个或更多时改变背景颜色。
我怎样才能做到这一点?
解决方案
首先,您应该为这两种状态添加配置,无论 tableviewCell 是否满足isTapsEnough
条件。
var numberOfTaps: Int = 0
var isTapsEnough: Bool { retrun numberOfTaps >= 3 }
func tap() {
self.numberOfTaps += 1
if self.isTapsEnough {
self.tableView.reloadData()
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.contentView.backgroundColor = self.isTapsEnough ? .yellow : .clear
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) -> UITableViewCell {
self.tap()
}
推荐阅读
- javascript - NodeJS,从另一个应用程序复制文本
- python-2.7 - 处理字典列表列表中的 None
- node.js - 节点包在本地工作,但在部署到 Heroku 时不能
- angular - 延迟加载模块中的 Angular Material 对话框
- c# - 如何配置控制器返回 null 而不是 Guid.Empty?
- selenium - 如果不是 utf 8,Docker 中的 Chrome 无头文件名错误
- php - Laravel 表单不发送任何数据
- php - 在 Laravel 6.0 上安装 Debugbar 后 Laravel 路由消失了
- java - 什么类型的三角形?
- c# - 如何制作并行任务并等待任务完成