How to customize the header of a TableView section using XIB?

I am trying to create custom section headers for a UITableView. I found some links illustrating how to do this completely in code (# 1349571 ).

I am trying to determine if it is possible to create a UIView in Interface Builder and use it to customize the title, similar to how it can be done for UITableViewCell?

+6
source share
3 answers

YES You can use a header created using XIB. Create an XIB and a class to control the XIB (class UIView). Use

YourNibClassName* v = [[[NSBundle mainBundle] loadNibNamed:@"YOUR_XIB_NAME" owner:self options:nil] firstObject]; //With this method you can load any xib for header view tableView.tableHeaderView = v; [v release]; 

EDIT

Return this view to viewForHeaderInSection like this

 YourNibClassName* v = [[[NSBundle mainBundle] loadNibNamed:@"YOUR_XIB_NAME" owner:self options:nil] firstObject]; //Do some stuff here like setting text on labels etc. return [v autorelease]; 
+7
source

Since we have prototype cells, you can also add a prototype cell to your table. Fill in the prototype cell ID in Interface Builder. (e.g. HeaderCell) Then you can use it in viewForHeaderForSection, as you use cells in cellForRowAtIndexPath.

Example:

 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [tableView dequeueReusableCellWithIdentifier:HeaderCellID]; UILabel *label = (UILabel *)[headerView viewWithTag:100]; label.text = [self tableView:self.tableView titleForHeaderInSection:section]; return headerView; } 
+3
source

add it as labels above the table and delete the stuck table title. I did it as soon as I know it sounds weird, but it works

0
source

Source: https://habr.com/ru/post/890456/


All Articles