I'm trying to use a pod called SwipeCellKit in my project, where this pod add a class called SwipeTableViewCell. What I have done so far is:
UIViewController class.SwipeCellKit to the Swift file.numberOfRows and cellForRow function to my UIViewController class.This is the implementation of the cellForRow:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SwipeTableViewCell;
return cell;
}
But I got error on the let cell = ..., saying:
Could not cast value of type 'UITableViewCell' to 'SwipeCellKit.SwipeTableViewCell'
Why? And how to fix this? I think I have arranged everything as it should be. Did I miss something?
What solved the problem for me:
In Main.Storyboard (or where you have your views), make sure that everywhere you want to use the reusable "cell", in the Identity Inspector on the right pane, you have the following:
You need to create your own Cell class as subclass of SwipeTableViewCell then you need to implement SwipeTableViewCellDelegate protocol in your ViewController
and for this line
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SwipeTableViewCell
you should use your class name which is a SwipeTableViewCell subclass
example
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! MySwipeableCell
cell.delegate = self
I created the following example
import UIKit
import SwipeCellKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! SwipeTableViewCell
return cell
}
}
class SwipeTableViewCellSubClass: SwipeTableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
set the class for your view to be SwipeTableViewCellSubClass.
this should work perfectly for you. good luck