0

我更改了 textlabel 颜色,didSelectRowAt但是当我滚动它时,它也会UITableView影响其他textlabel

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

2 回答 2

1

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

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

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

在此之后,当用户在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
}

最后管理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
    }
}

此代码用于一次选择单元格,因此您必须设置

tableView.allowsMultipleSelection = false

希望这可以帮助。

于 2018-10-11T06:29:05.530 回答
0

这应该适合你。

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移至UITableViewCellunderfunc setSelected(_ selected: Bool, animated: Bool)方法

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

        // label textColor logic goes here
        // make use of selected
    }
}
于 2018-10-11T06:14:47.443 回答