uchicago-mobi / 2016-Winter-Forum

Class Forum for MPCS51030.
3 stars 0 forks source link

Reusing prototype cells #231

Closed ghost closed 8 years ago

ghost commented 8 years ago

A cell created in storyboard as a prototype cell for one table view can be programmatically instantiated as a prototype cell for a different table view, but its UIImageViews and UILabels (etc.) cannot be accessed from the different table view. The workaround is to create another prototype cell and TableViewCell subclass, but that's a lot of code duplication--is there some way to reuse the original class without duplicating?

hbennett766 commented 8 years ago

What about a table view cell superclass? I created a class called DesignTableViewCell, and put all of the design work that you're talking about (image views, labels, etc) in there. Then instead of:

ExampleTableViewCell: UITableViewCell {} OtherTableViewCell: UITableViewCell {}

have them inherit the design class:

ExampleTableViewCell: DesignTableViewCell {} OtherTableViewCell: DesignTableViewCell {}

It doesn't completely eliminate code duplication, but it lessens it.

ghost commented 8 years ago

Hmm, okay. I tried that refactor and it didn't work, so there's something I've gotta figure out. I'll do a workaround for now & probably update the app after the deadline.

susanstevens commented 8 years ago

@manglano You can use the same custom table view cell class for multiple view controllers.

When you add IBOutlets to your custom table view cell class, you drag from the storyboard to the class. You can add additional outlets to different view controllers by dragging in the opposite direction -- from the circle that appears to the left of the IBOutlet in the custom cell class to the label (or image view, button, etc.) in storyboard.

(You can't see where my mouse is in this screenshot, but I'm dragging from the IBOutlet in code towards the label in storyboard.)

screen shot 2016-03-16 at 7 44 50 pm

ghost commented 8 years ago

Great! I will keep that in mind for the next iteration, other apps, etc.