简体   繁体   中英

Issue with Swift subclassing a class

I have a table view cell create like this where the code crashes on:

let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier") as! TestedViewCell;
cell.delegate = self;

I have the code to create the subclass like so:

class TestedViewCell: SwipeTableViewCell
{
    var animator: Any?
}

The above lines are in the same file with the kit imported. I then get the error like so:

Could not cast value of type 'SwipeCellKit.SwipeTableViewCell' (0x100121a60) to 'app.TestedViewCell' (0x100076f40).

Did I subclass it wrong? Thanks.

尝试注册TestedViewCell在ViewDidLaod

self.tableView.registerClass(TestedViewCell.self, forCellReuseIdentifier: "reuseIdentifier")

From your error

Could not cast value of type 'SwipeCellKit.SwipeTableViewCell' (0x100121a60) to 'app.TestedViewCell' (0x100076f40).

it is clear that your are trying to Cast SwipeTableViewcell as TestedViewCell. TestedViewCell is subclass of your SwipeTableViewcell . Issue might be in your register cell. You need to register cell in viewDidLoad as

self.tableView.registerClass(TestedViewCell.self, forCellReuseIdentifier: "testedViewCell")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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