Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to create custom UITableview section header in storyboard

Current I am creating a prototype cell in storyboard and using this cell as a section header. Inside tableView:viewForHeaderInSection: method, I am dequeuing the cell and returning it.

My section header cell has a UITextField and a UIButton in it. When I tap on text field keyboard appears but as soon as focus is moved away from text field whole section header disappears. This happens when I return the cell directly as section header view, but if I return a newly allocated UIView as section header view onto which cell is added as subview then everything works fine besides autoresizing masks.

Why header is disappearing?

I am not sure what could be the best thing todo here.

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    static NSString *CellIdentifier = @"SectionHeader"; 

SettingsTableViewCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //return sectionHeaderCell; // returning cell directly, section header disappears when focus is moved away from text field.
UIView * headerView = [[UIView alloc] initWithFrame:sectionHeaderCell.frame];
[headerView addSubView:sectionHeaderCell];
return sectionHeaderCell;//header view never disappears, but auto resizing masks do not work. Need to know how to set autoresizing masks to headerView so that it resizes correctly.

}  
like image 221
dev gr Avatar asked Dec 20 '14 05:12

dev gr


People also ask

How to add header and footer in tableView in swift?

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.

What is UITableView in Swift?

A view that presents data using rows in a single column. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+


2 Answers

I had the same issue and the fix was to return the cell's contentView like:

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader"; 

   SettingsTableViewCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   sectionHeaderCell.myPrettyLabel.text = @"Greetings";
   sectionHeaderCell.contentView.backgroundColor = [UIColor whiteColor]; // don't leave this transparent

   return sectionHeaderCell.contentView;
}  

And you get the same autolayouted results as before, but without the disappearing.

like image 38
Laszlo Avatar answered Oct 10 '22 07:10

Laszlo


Prototype cell table views only allow you to design cells in the storyboard editor, not section headers and footers. Your attempt to use a UITableViewCell as the section header is a clever hack, but it's just not supported by the classes involved—UITableViewCell is not designed to be used for anything other than a table view cell. It could do a lot worse than the view disappearing or not being laid out correctly; UIKit would be well within its rights to fail an assertion, delete all the app's data, revoke your developer certificate, or set your house on fire.

If you want your code to function properly, your choices are to either create your section headers in code or to put them in a separate XIB file. I know that's not what you want to do, but those are the options you have.

like image 76
Becca Royal-Gordon Avatar answered Oct 10 '22 05:10

Becca Royal-Gordon