代码之家  ›  专栏  ›  技术社区  ›  user8893378

更改tableView中单元格的editingStyle背景色和标签

  •  1
  • user8893378  · 技术社区  · 8 年前

    我有一个tableview,试图在其中实现滑动删除操作。

    我使用的是swift 4和Xcode 9。

    到目前为止,我有下一个代码,允许我在刷卡时显示删除标签:

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        switch editingStyle {
        case .delete:
            print("deleting row")
    
        default:
            return
        }
    }
    

    到目前为止还不错,现在我想把背景红色改成另一种颜色,如果可能的话,我想显示一个图标而不是标签。

    我正在尝试我在Swift 4中看到的代码,但在创建按钮时它会抱怨:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
            self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
            return
        }
        deleteButton.backgroundColor = UIColor.black
        return [deleteButton]
    }
    

    它给出了此错误->对成员“tableView(\uu:numberofrowsinssection:)”的引用不明确

    我不知道我是在尝试好的编辑方式,还是在swift 4上根本无法编辑。

    感谢您的帮助,

    谢谢

    3 回复  |  直到 8 年前
        1
  •  5
  •   BatyrCan    8 年前

    我使用的是Xcode 9.2和swift 4。使用了相同的代码,但没有错误。完整代码在这里。那可能对你有帮助。

        import UIKit
    
        class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
        var tableView: UITableView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height), style: .plain)
    
            tableView.dataSource = self
            tableView.delegate = self
    
            self.view.addSubview(tableView)
        }
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 1
        }
    
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return 3
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = UITableViewCell(style: .default, reuseIdentifier: "Cell")
            cell.textLabel?.text = "Lalala"
            return cell
        }
    
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }
    
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
            switch editingStyle {
            case .delete:
                print("deleting row")
    
            default:
                return
            }
        }
    
        func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
            let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
                self.tableView.dataSource?.tableView!(self.tableView, commit: .delete, forRowAt: indexPath)
                return
            }
            deleteButton.backgroundColor = UIColor.black
            return [deleteButton]
        }
    }
    
        2
  •  1
  •   Malleswari    8 年前

    我是这样尝试的,也许这对你有帮助。

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
                let deleteAction = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
    
                    print("Did tap the button")
                    self.deleteSession(sessionId: self.tableModel[indexPath.row].Id)
                    print("the session id is '\(self.tableModel[indexPath.row].Id)'")
                    self.tableModel.remove(at: indexPath.row)
                    print("\(indexPath.row)")
                    tableView.deleteRows(at: [indexPath], with: .fade)
                    print("\([indexPath])")
            self.medilogTableView.reloadData()
    
                }
        deleteAction.backgroundColor = UIColor.red
                //deleteAction.backgroundColor = UIColor(patternImage: UIImage(named: "delete_button")!)
                //renameAction.backgroundColor = UIColor(red: 0.298, green: 0.851, blue: 0.3922, alpha: 1.0)
                return [deleteAction]
            }
    
            medilogTableView is name of the table
    
        3
  •  0
  •   Clegrand    8 年前

    您必须在“in”回调中实现编辑代码:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        let deleteButton = UITableViewRowAction(style: .default, title: "Delete") { (action, indexPath) in
            // here implement your delete code
        }
    
        deleteButton.backgroundColor = .blue
        return [deleteButton]
    }
    

    “删除代码”的实现示例:

    self.tableView.beginUpdates()
    self.data.remove(at: indexPath.row)
    self.tableView.deleteRows(at: [indexPath], with: .left)
    self.tableView.endUpdates()
    
    推荐文章