Ios 文本标签颜色在我滚动UiTableView时自动更改

Ios 文本标签颜色在我滚动UiTableView时自动更改,ios,swift,uitableview,textlabel,Ios,Swift,Uitableview,Textlabel,我在didSelectRowAt上更改文本标签颜色,但当我滚动UITableView时,它也会影响其他文本标签的颜色 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let cell = tableView.cellForRow(at: indexPath) as! TableViewCell if (cell.LBLIntrest.textColor == (UICol

我在
didSelectRowAt
上更改文本标签颜色,但当我滚动
UITableView
时,它也会影响其他
文本标签的颜色

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


    if (cell.LBLIntrest.textColor == (UIColor.black))
    {
         cell.LBLIntrest.textColor = Uicolor.blue
    } else {
          cell.LBLIntrest.textColor = Uicolor.black
    }
}

这应该对你有用

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
    }    
}
但是,我建议将
cell.LBLIntrest.textColor=Uicolor.blue
移动到
func-setSelected(uuu-selected:Bool,animated:Bool)
方法下的
UITableViewCell

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

        // label textColor logic goes here
        // make use of selected
    }
}

首先,您必须创建属性来保存选定的单元格,如下所示

/* To hold selected cell */
var selectedIndexPath :IndexPath?
之后,在单元格中设置选定单元格的颜色

 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()
}
在此之后,当用户在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
}
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
    }
}
最后一次管理取消了rowat

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
}
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
    }
}
此代码用于一次选择单元格,因此您必须设置

tableView.allowsMultipleSelection = false

希望这能有所帮助。

之所以会出现这种情况,是因为当您滚动UITableView时,单元格会出列并被tableView重用。因此,您必须在cellForRowAt delegateI中将该单元格的颜色重置为初始颜色。我还添加了一个答案,请检查。@AbdulRehmanWarraich yup bro其工作谢谢,但是多选呢?