Add custom title to UITableView using storyboard

I can't seem to add a title to the UITableView using storyboard. I have a UITableView with several cell prototypes that appear and work fine. Then I dragged a new UIView over these prototype cells and added a label to it to act as the title of my table (NOT as a section title). I have created a new UIView subclass with one single property, which is UILabel. The UIView class in the storyboard is set to this custom UIView, and the UILabel output reference is set to the UILabel property of my custom UIView class.

Then, in my viewDidLoad UITableViewController method, I do the following:

DetailTableHeaderView *headerView = [[DetailTableHeaderView alloc] init];
headerView.entryNameLabel.text = @"TEST";
self.tableView.tableHeaderView = headerView;
[self.tableView reloadData];

      

But when I run my application, the table title is not displayed at all. I also noticed that the text header.entryNameLabel property is not even set to "TEST" as it should be.

What am I doing wrong here?

+3


source to share


2 answers


Old question, but I am providing my answer for reference reasons, as it is still a bit nontrivial how to add a table title using a storyboard for the design part.



  • Add a prototype cell to your tablet view in your storyboard.
  • By selecting the selected cell of the prototype, in the attribute index on the right, you will get the identifier (ex headerViewCell), as this will be a reference to it in order to use it.
  • Now click on the size inspector tab and give it a row height (this will be your header height)
  • In the code, now in the controller that handles the table view:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.tableView.tableHeaderView = [self.tableView dequeueReusableCellWithIdentifier:@"headerViewCell"];
    }
    
          

+1


source


This question is very old, but writing this answer in hopes it will help someone.

A good way to handle table headerView is to use delegate methods. Implement delegate methods tableView:viewForHeaderInSection

and tableView:heightForHeaderInSection

. Return your UIView from the method tableView:viewForHeaderInSection

.

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

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
}

      

Another option is to use a prototype cell (custom cell) to represent the header and return it to the tableView: viewForHeaderInSection method. See below code (I haven't tested this one yet):



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

    HeaderView *headerView = [self.TableView dequeueReusableHeaderFooterViewWithIdentifier:@"tableHeader"];

    // Set Text
    headerView.headerLabel.text = @"Some title";

    return headerView.contentView;
}

      

UPDATE

The above also works for the tableView: viewForHeaderInSection: method. Here's some sample code:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewCell *sectionHeader;
    CustomSectionHeaderCell *sectionHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"sectionHeaderCell"];

    // do stuff here

    return sectionHeaderCell.contentView;
}

      

0


source







All Articles