簡體   English   中英

如何在iOS Swift的TableView的可見單元格中更改文本的顏色

[英]How to change color of the text in visible cell of tableview in ios swift

在按下筆按鈕時,我可以更改主題的顏色,如您所見。

在此處輸入圖片說明 在此處輸入圖片說明 在此處輸入圖片說明

現在,我想更改可見單元格中的文本,使其看起來像其余的顏色。 如何更改可見單元格文本的顏色。

當您的單元格在屏幕上可見時,請使用以下委托方法。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{        
     // call visible on the screen.       
}

您可以使用UITableviewCell的 delegate方法

當單元格出現在用戶面前時被調用。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
     //Set highlighted color here
}

當單元格消失給用戶時調用。

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    //Set normal color here
}

像這樣創建一個ViewController

class ViewController: UIViewController {

    var color: UIColor = UIColor.red

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableView.dequeueReusableCell(withIdentifier: "Identifier") as! CustomCell

        cell.labelToChange.textColor = color

        return cell
    }
}

在筆上單擊,將color更改為其他顏色,然后調用tableView.reloadData()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM