【发布时间】:2018-12-18 02:43:18
【问题描述】:
我在TableViewCell 上创建了一个按钮。
当按下按钮时,颜色会连续变为白色和蓝色。
而ViewController有一个按钮可以改变所有cells的颜色。
在ViewController:
btnAllCheckBox.onTap
{ (tap) in
let sections = self.tableView.numberOfSections
var rows = 0
for i in 0..<sections {
rows += self.tableView.numberOfRows(inSection: i)
}
if self.btnAllCheckBox.backgroundColor == UIColor(hex: "#FFFFFF") {
for i in 0..<rows {
let cell = self.tableView.cellForRow(at: IndexPath(row: i, section: 0)) as? CustListTableViewCell
cell?.btnCheck.backgroundColor = UIColor(hex: "#58E5E4")
}
self.btnAllCheckBox.backgroundColor = UIColor(hex: "#58E5E4")
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "CustListTableViewCell", for: indexPath) as! CustListTableViewCell
cell.btnCheck.backgroundColor = UIColor(hex: "#FFFFFF")
return cell
}
在TableViewCell:
btnCheck.onTap
{ (tap) in
if self.btnCheck.backgroundColor == UIColor(hex: "#FFFFFF")
{
self.btnCheck.backgroundColor = UIColor(hex: "#58E5E4")
}
else
{
if let myViewController = self.parentViewController as? CustViewController {
myViewController.btnAllCheckBox.backgroundColor = UIColor(hex: "#FFFFFF")
}
self.btnCheck.backgroundColor = UIColor(hex: "#FFFFFF")
}
}
错误列表:
这是第一种情况。 (Cellcount = 1000)
当我按下全选按钮时,我的单元格不会改变颜色。
我尝试使用print打印日志,但没有问题。
第二种情况。 (Cellcount = 1000)
如果我点击第二个cell按钮并拖动屏幕,选择的位置会改变! (所有Cells通用)
ex) row 2 btnCheck.backgroundColor = #58E5E4
-> Drag Screen
-> row 2 btnCheck.backgroundColor = #FFFFFF
-> row 3,4 ... btnCheck.backgroundColor = #58E5E4
第三种情况:
如果我单击第 3 个单元格,13、23、... 颜色也会发生变化。
我认为我实现它没有问题,但我得到一个错误...请帮助我
【问题讨论】:
-
你没有数据源数组吗?像 struct CellModel { var bgColor: UIColor };让 arrayOfData: [CellModel];你必须拥有它。
-
@jpulikkottil 谢谢!!我设法创建和管理了一个数组变量,它成功了。
标签: ios swift uitableview tableview