【问题标题】:Change the color of all cell buttons in tableview更改表格视图中所有单元格按钮的颜色
【发布时间】: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


【解决方案1】:

首先,请注意UITableViewCell 是使用dequeueReusableCell 方法创建的,这意味着单元格将继续为每一行重复使用相同的UITableViewCell

因此,要对 UITableViewCell 进行更改,就是将 viewController 中变化的值保存在变量或对象中。然后在您的 cellForRowAt 方法中将遵循该值。

下面是一个例子(不完全是为了解决,但会帮助你解决问题)。

在你的 viewController 中,添加一个变量来改变颜色。

var dynamicColor: UIColor = .red

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "CustListTableViewCell", for: indexPath) as! CustListTableViewCell
    cell.btnCheck.backgroundColor = dynamicColor
    return cell
}

在您的触发方法中,您可以更改 dynamicColor 变量。

btnCheck.onTap
{ (tap) in
    if self.btnCheck.backgroundColor == .red
    {
        self.btnCheck.backgroundColor = .blue
        dynamicColor = .blue

    }
    else if self.btnCheck.backgroundColor == .blue
    {
        self.btnCheck.backgroundColor = .red
        dynamicColor = .red
    }

    tableView.reloadData()
}

并且不要忘记在每次更改后重新加载 tableView 以反映行

tableView.reloadData()

【讨论】:

    猜你喜欢
    • 2011-04-27
    • 2018-10-04
    • 2015-02-24
    • 1970-01-01
    • 2018-04-19
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多