Enkha
Enkha

Reputation: 430

Change the color of all cell buttons in tableview

I created a button on the TableViewCell.

When the button is pressed, the color changes continuously to white and blue.

And ViewController has a button to change the color of all cells.

In 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
}

In 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")
    }
}

List of bugs:

It's the first situation. (Cell count = 1000)

I have a cell that does not change color when I press the Select All button. I tried to print the log using print, but there was no problem.

Second situation. (Cell count = 1000)

If I click on the second cell button and drag the screen, the selected position will change! (Common to all Cells)

ex) row 2 btnCheck.backgroundColor = #58E5E4
-> Drag Screen
-> row 2 btnCheck.backgroundColor = #FFFFFF
-> row 3,4 ... btnCheck.backgroundColor = #58E5E4

Third case:

If I click 3rd cell, 13, 23, ... color changes as well.

I think I implemented it without problems, but I get an error... Please help me

Upvotes: 0

Views: 445

Answers (1)

xmhafiz
xmhafiz

Reputation: 3538

First, take note that UITableViewCell is created with dequeueReusableCell method, which means the cell will keep reuse the same UITableViewCell for each rows.

Thus, in order to make changes of UITableViewCell is to keep the changing values in viewController in variables or objects. Then in your cellForRowAt method will just follow the value.

Below is an example (not exactly to solve, but will help your problem).

In your viewController, add a variable to change the color.

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
}

In your triggering method, you can make changes to that dynamicColor variable.

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()
}

And dont forget to reload the tableView after each changes to reflect the rows

tableView.reloadData()

Upvotes: 1

Related Questions