簡體   English   中英

按下按鈕即可更改行的顏色-Swift

[英]Changing the colour of a row on button press - swift

我有以下代碼,當向UITableView行向左滑動時,兩個按鈕會出現。

override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .normal, title: "Picked") { action, index in
        print("Stock Picked")

    }
    more.backgroundColor = .green

    let favorite = UITableViewRowAction(style: .normal, title: "Not Enough") { action, index in
        print("Not enough stock to pick")
    }
    favorite.backgroundColor = .orange



    return [favorite, more]
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    return true
}

我現在正在嘗試使它變成一個按鈕,如果按下其中一個按鈕,它所顯示的行將改變顏色。 即,如果向左滑動一行並按下Picked按鈕,則該行的顏色將變為綠色。

我還希望它在行上設置某種標志,僅當所有行的標志都設置為true時,才允許應用程序向前移動。

任何建議,不勝感激。

要在編輯操作中更改單元格顏色,應使用UITableViewcellForRowAtIndexpath方法獲取單元格並將背景色更改為所需的顏色,這必須在UITableViewRowAction提供的操作塊內完成

override func tableView(_ tableView: UITableView, editActionsForRowAt: IndexPath) -> [UITableViewRowAction]? {
    let more = UITableViewRowAction(style: .normal, title: "Picked") { action, index in
        print("Stock Picked")
        let cell = tableView.cellForRow(at: index) as? UITableViewCell
        cell?.backgroundColor = .green
    }
    more.backgroundColor = .green

    let favorite = UITableViewRowAction(style: .normal, title: "Not Enough") { action, index in
        print("Not enough stock to pick")
        let cell = tableView.cellForRow(at: index) as? UITableViewCell
        cell?.backgroundColor = .orange
    }
    favorite.backgroundColor = .orange


    return [favorite, more]
}

暫無
暫無

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

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