UITableViewHeaderFooterView in InterfaceBuilder
Following workarounds enable me to drag-assign IB items to code as variables. UITableViewHeaderFooterView doesnt allow that out of the box.
create a (New File/CocoaTouchClass) UITableViewHeaderFooterView .h.m.xib normally
temporarily rename superclass from UITableViewHeaderFooterView to UIView. Drag-assign your UI items to code as needed, IB will assign key-value correctly, revert back to UITableViewHeaderFooterView when done.
- in your tableview, use registerNib: to register instead of registerClass:. prepare the rest of tableview normally (ie:dequeue).
It's absolutely possible and Apple provides an example here.
Download the sample code and look at the SectionHeaderView.xib.
The way to do it is to create a xib with a single UIView on it. Then, set the class type to a class that inherits from UITableViewHeaderFooterView.
Once you have a nib with a class that inherits from UITableViewHeaderFooterView, call the following to register the class for reuse as a header or footer:
static NSString * const SectionHeaderViewIdentifier = @"SectionHeaderViewIdentifier";
[self.tableView registerNib:[UINib nibWithNibName:@"SectionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];
To put the view into use, implement the table delegate method tableView:viewForHeaderInSection: like so:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSinteger)section {
SectionHeaderViewClass *sectionHeaderView = (SectionHeaderView *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
// Do stuff...
return sectionHeaderView;
}
While all these answers work, they don't quite address the original question of how to set the reuseIdentifier
using Interface Builder. It is, indeed, possible with the help of "User Defined Runtime Attributes". You can set the reuseIdentifier
directly in Interface Builder using an attribute:
Doing this will accomplish exactly the same thing as the native reuseIdentifier
field in UITableViewCell
Interface Builder attribute inspector.
I have written a guide (tested on iOS 9), which comprise of 4 steps:
- Subclass
UITableViewHeaderFooterView
- Create Nib with the subclass view, and add 1 container view which contains all other views in the header/footer
- In
viewDidLoad
, callregisterNib:forHeaderFooterViewReuseIdentifier:
for the table view - Implement
viewForHeaderInSection
and usedequeueReusableHeaderFooterViewWithIdentifier
to get back the header/footer