简体   繁体   English

如何在 swift 的 tableView 中向左滑动时更改单元格颜色

[英]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.

相关问题 在 swift 中显示更多按钮时取消在 tableview 单元格上向左滑动 - Cancel swipe left on tableview cell when showing more buttons in swift 在Swift 3中滑动时如何向TableView添加向左滑动手势并返回单元格位置 - How to add a Swipe Left gesture to a TableView and return the cell location when swiped in Swift 3 如何在iOS Swift的TableView的可见单元格中更改文本的颜色 - How to change color of the text in visible cell of tableview in ios swift 如何在TAP上而不是滑动上编辑TableView单元格-Swift - How to edit tableview cell on TAP instead of swipe - Swift Swift 创建左、右滑动,长按 tableview 行单元格可移动 - Swift create left, right swipe with long press tableview row cell movable 迅速。选中后更改自定义Tableview单元格标签的颜色 - Swift. Change Color of Custom Tableview Cell Label when selected 当向左滑动表格视图单元格到手机的左边缘时,它将自动触发删除UITableViewRowAction,如何禁用此功能? - when left swipe a tableview cell to the left edge of the phone, it will auto trigger the delete UITableViewRowAction , how to disable this? 如何在tableView中创建自定义单元格(滑动单元格) - how to create custom cell in tableView (swipe cell) 如何在iOS SDK中的TableView中向左和向右滑动 - how to left and right swipe in tableview in iOS sdk 如何在Swift的滑动操作中更改UITableviewCell背景颜色? - How to change UITableviewCell background color on swipe action in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM