Reusing a UITableViewCell's IBOutlets with two different UITableViewControllers

277 views Asked by At

I'm creating a coffee timer as a learning exercise and I want to have a different menu based on the brewing method selected. I'm storing my settings in NSUserDefaults and populating my tables with two separate arrays that look like this:

var aero = ([
        "Cool Down Time Enabled" : true,
        "Cool Down Time" : 30,
        "Inverted" : true,
        "Base Steep Time" : 150,
        "Base Inverted Steep Time" : 180,

        ])

var french = ([
        "Cool Down Time Enabled" : true,
        "Cool Down Time" : 30,
        "Base Steep Time" : 180,

        ])

I created two UITableViewControllers, one for FrenchPress and one for AeroPress, they are largely the same code:

FrenchPressTableViewController.swift - settings menu

AeroPressTableViewController.swift - settings menu

Both assign values to instances of my Setting.Swift class (gist) and get stored in different arrays.

In IB I started with my french press controller and created two prototype cells and two prototype classes to go along with them.

SwitchCell.Swift - Model for my UISwitch prototype cell outlets

TimingCell.Swift - Models my TimingButton prototype cell outlets

Initial Steps taken:

  • Create new tableView in IB for my French Press settings.
  • Create a designated FrenchPressTableViewController, and assign it to my new French Press table view
  • Set up two new prototype cells and set up IBOutlets/Actions using my SettingCell & TimingCell classes.
  • The IBOutlets are connected from my FrenchPressTableViewController to my SettingCell.swift (I later reuse these outlets for my Aero settings).

My FrenchPress Settings Works Fine: enter image description here

So I decided to add my Aero press settings menu using the same process:

  • Create new tableView in IB
  • Create a AeroPressTableViewController, and assign it to my new aero table view
  • Set up two new prototype cells and assign my previously existing SettingCell & TimingCell to the proper cells.
  • I utilize the outlets that I previously connected to my FrenchPressTableViewController and I connect them to my Aero controller as well to reuse the cell classes.

The final product looks like like this:

enter image description here

But when I run my Aero settings menu, I get the following error:

fatal error: unexpectedly found nil while unwrapping an Optional value

enter image description here

enter image description here

Question:

From what I can tell from my debugger, it looks like my SettingCell class is returning a nil value with cell.switchLabel.text. This doesn't make sense to me since I'm trying to assign a value to it.

Clearly I'm doing something wrong, what is the proper way to reuse my UITableViewCell classes?

1

There are 1 answers

0
LC 웃 On BEST ANSWER

This issue arises while registering the UITableViewCell class as tableView.registerClass(SwitchCell.self,forCellReuseIdentifier:"SwitchCell2")

Remove that line tableView.registerClass(SwitchCell.self,forCellReuseIdentifier:"SwitchCell2") and set the cellIdentifier in storyboard..it works