[英]How to change cell color on swipe left in tableView in swift
I have 3 action button in a tableview and I want to change the background color of current cell on swipe and preselected cell should be reverted if I swipe another cell, background color of only current cell should be changed.我在表格视图中有 3 个操作按钮,我想在滑动时更改当前单元格的背景颜色,如果我滑动另一个单元格,则应还原预选的单元格,仅应更改当前单元格的背景颜色。 I am using trailingSwipeActionsConfigurationForRowAt delegate for swipe actions, but I don't know how to change the background color of the current swiped cell, any idea please help me out.
我正在使用trailingSwipeActionsConfigurationForRowAt代表进行滑动操作,但我不知道如何更改当前滑动单元格的背景颜色,任何想法请帮助我。
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let viewButton = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in
// View action
print("View action")
}
viewButton.backgroundColor = .gray
viewButton.image = UIImage(named: "view")
//Approve
let approveButton = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in
// Approve action
print("Approve action")
}
approveButton.backgroundColor = .gray
approveButton.image = UIImage(named: "accept")
//Reject
let rejectButton = UIContextualAction(style: .normal, title: "") { (contextualAction, view, boolValue) in
// Reject action
print("Reject action")
}
rejectButton.backgroundColor = .gray
rejectButton.image = UIImage(named: "reject")
var swipeActions = UISwipeActionsConfiguration(actions: [rejectButton, approveButton, viewButton])
swipeActions.performsFirstActionWithFullSwipe = false
return swipeActions
}
There are a million way to implement the data model.有一百万种方法可以实现数据 model。 Just to give you an idea, you can change the tableViewCell background color you can use
tableView.cellForRow(at: indexPath
:只是给你一个想法,你可以改变你可以使用
tableView.cellForRow(at: indexPath
:
var selectedCell:IndexPath? //this hold the index path of the selected cell
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
// your code here .....
if let cell = tableView.cellForRow(at: indexPath) {
if selectedCell == nil {
print("selected cell is nil")
//first selection
cell.backgroundColor = .red
selectedCell = indexPath
}else{
if tableView.indexPath(for: cell) != selectedCell {
// you are selecting another cell
cell.backgroundColor = .red
tableView.cellForRow(at: selectedCell!)?.backgroundColor = nil
selectedCell = indexPath
}
}
}
}
func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)
cell?.contentView.backgroundColor = .red
}
func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
if indexPath != nil {
let cell = tableView.cellForRow(at: indexPath!)
cell?.contentView.backgroundColor = .red
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.