TextLabel colour automatically changed when i Scroll UiTableView

前端 未结 2 689
名媛妹妹
名媛妹妹 2021-01-27 06:04

I changing textlabel color on didSelectRowAt but when I scroll UITableView it also effects in other textlabel also

func ta         


        
2条回答
  •  春和景丽
    2021-01-27 06:36

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

提交回复
热议问题