After many years of avoiding Interface Builder like the plague I decided to give it a chance. It's not easy.
Take UITableViewHeaderFooterView
for example. Like UITableViewCell
, it has a contentView
property. Unlike UITableViewCell
, it doesn't have a template in the Interface Builder object library.
How are we supposed to use Interface Builder to create a UITableViewHeaderFooterView
with the content inside contentView
? The fact that registerNib:forHeaderFooterViewReuseIdentifier:
exists makes me think this should be possible somehow.
I just did this with a footer and a NIB file:
UITableViewHeaderFooterView
view to Default.In your UITableViewController
viewDidLoad
register the NIB file to be used with a reuse identifier:In your UITableViewController's
tableView:viewForFooterInSection:
use the Footer identifier to fetch and return the view:I found easier way.
1) Create subclass of UITableViewCell and set your xib file
2) In your header file change superclass from UITableViewCell to UITableViewHeaderFooterView
That's it.
This is the closest I got to define a
UITableViewHeaderFooterView
with IB:a. Create a
UITableViewHeaderFooterView
subclass (MYTableViewHeaderFooterView
).b. Create a nib file for the
contentView
only (MYTableViewHeaderFooterContentView
).c. Override
initWithReuseIdentifier:
inMYTableViewHeaderFooterView
to load the view defined in the nib file.d. Register the
MYTableViewHeaderFooterView
class instead of the nib file:Just use the UITableViewCell template in IB. Change the class to UITableViewHeaderFooterView. Here you have it... with a contentView.
This solution works well, especially if you want it to work correctly in relation to Readable Content Guides (introduced in iOS 9). Instead of creating a UITableViewHeaderFooterView, it simply returns a custom UIView (from a XIB) when it is required:
Create a new class that subclasses UIView ("AwesomeHeaderView") and create your outlets:
In your UIViewController (or UITableViewController) call the following:
An awful hack I figured out is to create a IBOutlet contentView ih your headerFooter class and hook it up to the your "content view" in the xib (Have your xib laid out like a tableViewCell, View->contentView->mystuff).
You'll get warnings, ok ready for the hack...
Delete the IBOutlet and it will all work.