Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewHeaderFooterView in InterfaceBuilder

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....

like image 641
Peter Lapisu Avatar asked Jul 15 '13 10:07

Peter Lapisu


People also ask

How to Add header in tableView?

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.

How can I make the Footerview always stay at the bottom in Uitableviewcontroller?

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.


1 Answers

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; } 
like image 128
Matt Becker Avatar answered Oct 17 '22 20:10

Matt Becker