TextLabel colour automatically changed when i Scroll UiTableView

对着背影说爱祢 提交于 2019-12-02 10:34:17

First you have to create property to hold selected cell like below

/* To hold selected cell */
var selectedIndexPath :IndexPath?

After that set color of selected cell in cellForRowAt

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if let cell = tableView.dequeueReusableCell(withIdentifier: "cell") {
        cell.textLabel?.text = "Row Number: \(indexPath.row)"

        /* Check if cell is selected then set layout accourding to your requirements */
        if indexPath == selectedIndexPath {
            cell.textLabel?.textColor = .blue
        } else {
            cell.textLabel?.textColor = .black
        }
        return cell
    }

    return UITableViewCell()
}

After this manage when user select a cell in didSelectRowAt

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // Toggle if user seleted same cell
    if selectedIndexPath == indexPath {
        if let cell = tableView.cellForRow(at: indexPath) {
            /* Check and toggle selected cell color */
            cell.textLabel?.textColor = cell.textLabel?.textColor == .black ? .blue : .black
        }
    } else {
        /* set color of seleted cell */
        if let cell = tableView.cellForRow(at: indexPath) {
            cell.textLabel?.textColor = .blue
        }
    }

    /* Save which cell is selected */
    selectedIndexPath = indexPath
}

And last manage didDeselectRowAt

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    /* Remove if deselect same cell */
    if selectedIndexPath == indexPath {
        selectedIndexPath = nil
    }
     /* Change color to black */
    if let cell = tableView.cellForRow(at: indexPath) {
        cell.textLabel?.textColor = .black
    }
}

This code is for on cell selection at one time so you have to set

tableView.allowsMultipleSelection = false

Hope this helps.

This should work for you.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let cell = tableView.cellForRow(at: indexPath) as! TableViewCell
    setSelectedColor(cell: cell)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    ...
    ...

    if let selectedRows = tableView.indexPathsForSelectedRows, selectedRows.contains(indexPath) {
        setSelectedColor(cell: cell)
    }

    return cell
}


func setSelectedColor(cell: UITableViewCell) {
    if (cell.LBLIntrest.textColor == (UIColor.black)) {
        cell.LBLIntrest.textColor = Uicolor.blue
    } else {
        cell.LBLIntrest.textColor = Uicolor.black
    }    
}

But, I would recommend moving that cell.LBLIntrest.textColor = Uicolor.blue to UITableViewCell under func setSelected(_ selected: Bool, animated: Bool) method

class TableViewCell: UITableViewCell {
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // label textColor logic goes here
        // make use of selected
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!