문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top