简体   繁体   中英

Subclassing UITableViewCell and connecting outlets

I'm working in Swift 3 and Xcode 8.

I have a number of table view cell prototypes where the cells are very similar, but there are enough differences where I want to make a subclass of UITableviewCell to hold all the similar outlets and the basic initialization, but then create a few classes which subclass off of the new class.

Question is, how do I get the outlets for these cells all connected to the outlets in the base class?

I can do this for the first cell, but then IB sees those outlets as connected, and won't let me connect them from the next subclassed cell.

I copied the first prototyped cell and pasted it in, then changed it's class to the second type. This still showed the outlets connected, and this seems to work, however, an action for a button in the second cell is not working.

What's the trick to getting all the different subclassed cells to have their outlets and actions connected to the base cell class?

If you have different classes for different cell then take objects of those classes and by using them you will be able to use your different cell's label,button etc

let cell1 = tableView.dequeueReusableCellWithIdentifier("Cell1",forIndexPath: indexPath) as! YourSubClass1

let cell2 = tableView.dequeueReusableCellWithIdentifier("Cell2",forIndexPath: indexPath) as! YourSubClass2

You need to remove the outlets of the second cell and connect them again to the subclass outlet properties after changing the second class type.

If you don't remove the outlets, it will still refer to the outlets of the first cell and that won't work.

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