0

I want to use a custom UITableViewCell as tableHeaderView of my TableView. I have not found any example of this on SO, maybe it is not possible. I have tried several versions of the following code in viewDidLoad:

cNib = [UINib nibWithNibName:@"HeaderUserLoggedOutCell" bundle:nil];
[self.menuTableView registerNib:cNib forHeaderFooterViewReuseIdentifier:@"HeaderUserLoggedOutCell"];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(20, 0, self.menuTableView.frame.size.width, 40.0)];
HeaderUserLoggedOutCell *userLoggedOutCell = [[HeaderUserLoggedOutCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"HeaderUserLoggedOutCell"];
userLoggedOutCell.frame = CGRectMake(111, userLoggedOutCell.frame.origin.y+20, self.view.frame.size.width, userLoggedOutCell.frame.size.height);
[headerView addSubview:userLoggedOutCell];
self.menuTableView.tableHeaderView = headerView;

The labels that are in my Xib file don't appear. I think that this Nib registration is more for section headers but I am not sure, so I tried.

Thank you

aiwis31
  • 154
  • 2
  • 8

1 Answers1

1

Not sure how you're setting up your custom cell, but if it has a nib associated with it, you can do this:

HeaderUserLoggedOutCell *userLoggedOutCell = [[[NSBundle mainBundle] loadNibNamed:@"HeaderUserLoggedOutCell" owner:self options:nil] objectAtIndex:0];
// customize your cell in whatever way you need to
self.menuTableView.tableHeaderView = userLoggedOutCell.contentView;

No need to add your custom cell to a separate UIView.

Stonz2
  • 6,306
  • 4
  • 44
  • 64
  • Actually I needed to add my cell to a separate UIView. I spent two days trying to understand why my cell was randomly appearing. I found the solution [here](http://stackoverflow.com/questions/12772197/what-is-the-meaning-of-the-no-index-path-for-table-cell-being-reused-message-i). – aiwis31 Apr 21 '14 at 17:52