Search code examples
swiftuitableviewcelluicolor

Change color cell in UITableView Previously recorded Swift


How can I change the color of cells that have been added?

When I change if the cell changes the color that will be added to the UITableView , I need to change the color of the cells in all the TableView .

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell_1") as! TableView_Cell
        let item = self.items[indexPath.row]
        cell.la_view.text = item.list_1
        cell.la_view2.text = item.list_2

    switch sw {
    case 0:
    cell.la_view.textColor = UIColor.green
    cell.la_view2.textColor = UIColor.green

    case 1:
    cell.la_view.textColor = UIColor.white
    cell.la_view2.textColor = UIColor.white

    default:
        print("")
    }
        cell.backgroundColor = UIColor(named: "Defeult")

        return cell
}

enter image description here


Solution

  • If you only have two colors, then you can just use a Boolean variable:

    var colorSwitch = false
    

    Then in your tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {:

    if colorSwitch {
        cell.la_view.textColor = //color
        cell.la_view2.textColor = //color
    else {
        cell.la_view.textColor = //color
        cell.la_view2.textColor = //color
    

    If you have more than one color to change to, you can have an integer value and use the switch case statement you have already.