SwipCellKit swift 5 - unable to swipe to launch the dele menu

59 Views Asked by At

I'm using Xcode 11.2 and Swift 5 with the latest version of SwipeCellKit.

In my code, I have a UITableView with cells in it. 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

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
    }
}
1

There are 1 best solutions below

0
Idanis On

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