Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewHeaderFooterView with IB

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.

like image 927
hpique Avatar asked Oct 03 '13 14:10

hpique


1 Answers

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"]; 
like image 153
hpique Avatar answered Sep 28 '22 11:09

hpique