Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a section header in UITableView

How can I add a section header in UITableView?

like image 711
Talktobijju Avatar asked Aug 10 '10 06:08

Talktobijju


People also ask

How do I add a section in Swift?

To add a section around some cells, start by placing a Section around it, optionally also adding a header and footer. As an example, we could create a row that holds task data for a reminders app, then create a list view that has two sections: one for important tasks and one for less important tasks.

How do I create a header in view?

Header and footer views are optional, and you can customize them as much or as little as you want. 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.


2 Answers

if you just want section header title, you can use UITableViewDataSource method:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

Or you can set you custom view as header using UITableViewDelegate method:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

The controller implementing these, needs to be the delegate/datasource of your table view.

like image 162
Swapnil Luktuke Avatar answered Oct 11 '22 05:10

Swapnil Luktuke


After using UITableViewDelegate method:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

you must also using UITableViewDelegate method:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

to display the header.

like image 43
Byron Avatar answered Oct 11 '22 05:10

Byron