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.
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:
in MYTableViewHeaderFooterView
to load the view defined in the nib file.
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithReuseIdentifier:reuseIdentifier]; if (self) { NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"MYTableViewHeaderFooterView" owner:self options:nil]; UIView *nibView = [objects firstObject]; UIView *contentView = self.contentView; CGSize contentViewSize = contentView.frame.size; nibView.frame = CGRectMake(0, 0, contentViewSize.width, contentViewSize.height); [contentView addSubview:nibView]; } return self; }
d. Register the MYTableViewHeaderFooterView
class instead of the nib file:
[self.tableView registerClass:[MYTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"cell"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With