I want to create a custom header section view in a separate .xib using IB
However in IB i cannot find a component UITableViewHeaderFooterView (similar to UITableViewCell) and assign a reuse for it
So how to create in custom header section?
I create a class MySection which inherits from UITableViewHeaderFooterView Create a .xib, MySection.xib Register xib for cell reuse
the problem is, how to use the initWitReuseIdentifier....
To create a basic header or footer with a text label, override the tableView(_:titleForHeaderInSection:) or tableView(_:titleForFooterInSection:) method of your table's data source object. The table view creates a standard header or footer for you and inserts it into the table at the specified location.
If you need to make the footer view fixed at the bottom then you can not use a TableViewController . You will have to use UIViewController , put your tableView as a subview. Put the footer also as another subview and its done.
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; }
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