Swift subclass 问题 class
Issue with Swift subclassing a class
我有一个像这样创建的 table 视图单元格,其中代码崩溃于:
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! TestedViewCell;
cell.delegate = self;
我有像这样创建子类的代码:
class TestedViewCell: SwipeTableViewCell
{
var animator: Any?
}
以上几行与导入的套件在同一个文件中。然后我得到这样的错误:
Could not cast value of type 'SwipeCellKit.SwipeTableViewCell' (0x100121a60) to 'app.TestedViewCell' (0x100076f40).
我是不是把它子类化错了?谢谢。
尝试在 ViewDidLoad
中注册 TestedViewCell
self.tableView.registerClass(TestedViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
来自你的错误
Could not cast value of type 'SwipeCellKit.SwipeTableViewCell'
(0x100121a60) to 'app.TestedViewCell' (0x100076f40).
很明显,您正在尝试将 SwipeTableViewcell 转换为 TestedViewCell。 TestedViewCell 是您的 SwipeTableViewcell 的子类。问题可能出在您的注册单元格中。您需要在 viewDidLoad 中将单元格注册为
self.tableView.registerClass(TestedViewCell.self, forCellReuseIdentifier: "testedViewCell")
我有一个像这样创建的 table 视图单元格,其中代码崩溃于:
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! TestedViewCell;
cell.delegate = self;
我有像这样创建子类的代码:
class TestedViewCell: SwipeTableViewCell
{
var animator: Any?
}
以上几行与导入的套件在同一个文件中。然后我得到这样的错误:
Could not cast value of type 'SwipeCellKit.SwipeTableViewCell' (0x100121a60) to 'app.TestedViewCell' (0x100076f40).
我是不是把它子类化错了?谢谢。
尝试在 ViewDidLoad
中注册TestedViewCell
self.tableView.registerClass(TestedViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
来自你的错误
Could not cast value of type 'SwipeCellKit.SwipeTableViewCell' (0x100121a60) to 'app.TestedViewCell' (0x100076f40).
很明显,您正在尝试将 SwipeTableViewcell 转换为 TestedViewCell。 TestedViewCell 是您的 SwipeTableViewcell 的子类。问题可能出在您的注册单元格中。您需要在 viewDidLoad 中将单元格注册为
self.tableView.registerClass(TestedViewCell.self, forCellReuseIdentifier: "testedViewCell")