Not setting File Owner class in Table View Cell XIB?


Hello all,

I’m creating the HomepwnerItemCell XIB file and the book on Kindle Location 7772 reads: “Finally, click on the cell in the outline view and select the identity inspector. Change the Class to HomepwnerItemCell”.

So far, in our XIB files, we were being instructed to change the File Owner’s class. I opened the example project provided with the sample code and File Owner should be NSObject, instead setting the Table View Cell class to HomepwnerItemCell.

I understand why we do this, because we’re gonna subclass the cell directly. But would give me the same results if we set the File Owner to HomepwnerItemCell instead of the Table View Cell itself, like we have been doing in the previous chapters?



Nope. The File’s owner exists solely to create connections between existing objects and objects being created by the XIB. This is how we got pointers from our existing objects (like a view controller) to our new objects (like a view).

UITableView gets its cells a little bit different. When a XIB file is unloaded, all of the objects in the left hand panel (called “top-level objects”) are put into an array, and that array is returned to whoever ran the code that loads the XIB file. A UITableView will simply grab the first object in that array and say, “Is this a cell? If so, I’ve got what I was looking for. If not, I’m going to crash your application.”

Thus, we do not need a connection from an existing object to the object archived in the XIB, because the UITableView handles that process itself.

Now, we still do need to change the class of the cell to HomepwnerItemCell. Otherwise, when the cell gets loaded from the XIB, it will be a UITableViewCell and NOT a HomepwnerItemCell. This means you won’t be able to connect any of the outlets from the cell to its subviews (because UITableViewCell doesn’t have an outlets) and the first time you send a HomepwnerItemCell message to the UITableViewCell, your app will crash.

The big thing to understand here is what a XIB file really is. Its just some objects that get allocated/initialized and one other object that has already been allocated/initialized. You get to set pointers and target-action pairs for all of these objects. The File’s Owner is an object you can use to set up pointers for objects that aren’t in the XIB, but its totally optional.