簡體   English   中英

如何在Swift的滑動操作中更改UITableviewCell背景顏色?

[英]How to change UITableviewCell background color on swipe action in Swift?

---------------------這是我的代碼---------------------

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        let important = importantAction(at: indexPath)
        //let delete = deleteAction(at: indexPath)

        return UISwipeActionsConfiguration(actions: [important])

    }

    @available(iOS 11.0, *)
    func importantAction(at indexPath: IndexPath) -> UIContextualAction
    {


        let action = UIContextualAction(style: .normal, title: nil) { (action, view, completion) in


            completion(true)
        }
        action.image = #imageLiteral(resourceName: "checked_wht")
        action.backgroundColor = UIColor.green

        return action
    }

試試這個代碼

@available(iOS 11.0, *)
func importantAction(at indexPath: IndexPath) -> UIContextualAction
{
    let action = UIContextualAction(style: .normal, title: nil) { (action, view, completion) in
        completion(true)
        let indexPath = IndexPath(item: indexPath.row, section: 0)
        self.tableView.reloadRows(at: [indexPath], with: .top)
    }
    let cell:UITableViewCell = (tableView.cellForRow(at: IndexPath(row: indexPath.row, section: 0)))!
    cell.backgroundColor = UIColor.red
    action.backgroundColor = UIColor.green

    return action
}

暫無
暫無

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

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