简体   繁体   English

SwipCellKit swift 5 - 无法滑动启动删除菜单

[英]SwipCellKit swift 5 - unable to swipe to launch the dele menu

I'm using Xcode 11.2 and Swift 5 with the latest version of SwipeCellKit .我将 Xcode 11.2 和 Swift 5 与最新版本的SwipeCellKit

In my code, I have a UITableView with cells in it.在我的代码中,我有一个UITableView ,其中包含单元格。 I wish to add swipe action from the right, which will let me use two actions: delete and edit.我希望从右侧添加滑动操作,这将让我使用两个操作:删除和编辑。

For now, I'm trying only the delete action.现在,我只尝试删除操作。 However, the swipe doesn't seem to be working, I get no actions at all.但是,滑动似乎不起作用,我根本没有采取任何行动。

What am I missing here?我在这里想念什么?

Here's my code:这是我的代码:

CartTableViewCell CartTableViewCell

import UIKit
import SwipeCellKit

class CartTableViewCell: SwipeTableViewCell
{
    @IBOutlet weak var mainView: UIView!
    @IBOutlet weak var productName: UILabel!
    @IBOutlet weak var productImage: UIImageView!
    @IBOutlet weak var priceForKg: UILabel!
    @IBOutlet weak var quantity: UITextField!
    @IBOutlet weak var subTotal: UITextField!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        self.layer.borderWidth = 1
        self.layer.cornerRadius = 25
        self.layer.borderColor = self.layer.backgroundColor

        self.mainView.layer.borderWidth = 1
        self.mainView.layer.cornerRadius = 25
        self.mainView.layer.borderColor = self.mainView.layer.backgroundColor

        self.quantity.layer.borderWidth = 1
        self.quantity.layer.borderColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        self.quantity.layer.cornerRadius = 5

        self.subTotal.layer.borderWidth = 1
        self.subTotal.layer.borderColor = #colorLiteral(red: 0.8078431487, green: 0.02745098062, blue: 0.3333333433, alpha: 1)
        self.subTotal.layer.cornerRadius = 5
    }
    override var frame: CGRect {
      get {
          return super.frame
      }
      set (newFrame) {
          var frame =  newFrame
          frame.origin.y += 4
          frame.size.height -= 2 * 5
          super.frame = frame
      }
    }
    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }
}

CartViewController购物车视图控制器

import UIKit
import SwipeCellKit

class CartViewController: UIViewController
{
    @IBOutlet weak var cartTableView: UITableView!

    var listOfProducts : [Product] = []

    override func viewDidLoad()
    {
        super.viewDidLoad()

        self.cartTableView.delegate = self
        self.cartTableView.dataSource = self
        self.cartTableView.separatorStyle = .none
        self.cartTableView.allowsSelection = false
        self.cartTableView.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)


        self.subTotalView.layer.borderWidth = 1
        self.subTotalView.layer.borderColor = self.subTotalView.layer.backgroundColor
        self.subTotalView.layer.cornerRadius = 10

        let cellNib = UINib(nibName: "CartTableViewCell", bundle: nil)
        self.cartTableView.register(cellNib, forCellReuseIdentifier: "cartProductCell")
    }
}

extension CartViewController : UITableViewDelegate, UITableViewDataSource, SwipeTableViewCellDelegate
{
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]?
    {
        guard orientation == .right else { return nil }

        let deleteAction = SwipeAction(style: .destructive, title: "Delete")
        {
            action, indexPath in
            // handle action by updating model with deletion
        }

        // customize the action appearance
        deleteAction.image = UIImage(named: "Delete")

        return [deleteAction]
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return listOfProducts.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cartProductCell", for: indexPath) as! CartTableViewCell
        return cell
    }
}

I found the answer, if any of you guys are looking for it, the missing line is to set the delegate of the cell before returning it:我找到了答案,如果你们中的任何人正在寻找它,缺少的行是在返回之前设置单元格的代表:

cell.delegate = self

As mentioned in this post: SwipeCellKit Swipe to delete not being called如本文所述: SwipeCellKit 滑动删除未被调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM