我正在使用 Xcode 11.2 和 Swift 5 以及最新版本的SwipeCellKit
.
在我的代码中,我有一个UITableView
with 单元格。我希望从右侧添加滑动操作,这将让我使用两个操作:删除和编辑。
现在,我只尝试删除操作。但是,滑动似乎不起作用,我根本没有采取任何行动。
我在这里想念什么?
这是我的代码:
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)
}
}
购物车视图控制器
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
}
}