Joe Joe - 3 months ago 9
Swift Question

How/where do I create a table view cell subclass and set it as the class of the prototype

I have a second viewcontoller and was linked to this


Create a table view cell subclass and set it as the class of the prototype. Add the outlets to that class and connect them. Now when you configure the cell you can access the outlets.


But I do not understand where I put the subclass and how to make one.


Outlets cannot be connected to repeating content iOS The link I was sent to.

Answer

First, create the file

The same way that you create a UIViewController or UITableViewController: In your project, go to New File. Select Cocoa Touch Class under iOS. The subclass is UITableViewCell and the name is whatever name you're giving it.

enter image description here

Within that class, you drag your buttons, labels, etc to create your outlets.


Assign the class to the cell

Remember to assign the class name to the cell in the identity inspector:

enter image description here


Put it in code

You will need a slight change in your cellForRowAtIndexPath method:

let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! MyCustomCell

where "myCell" is the identifier you've given your cell in the storyboard, and obviously MyCustomCell is the new class you've created to which you are casting your cell.