开发者

tableView.tableHeaderView being set but not drawn

whenever I set my tableHeaderView I'm not seeing it in the Simulator. If I add it as a subview, it ends up getting drawn underneath the section header. Any idea what I'm missing here?

I do have a XIB file. I didn't see any properties in IB to affect headerViews though.

- (void)viewDidLoad {   
[super viewDidLoad];
MyTitleView *titleView = [[MyTitleView alloc] initWithFrame:CGRectMake(60,0,260,40)];
titleView.label.text = @"My Title";
self.navigat开发者_如何学PythonionItem.titleView = titleView;
[titleView release];

StandardTableHeaderView *headerView = [[StandardTableHeaderView alloc] initWithFrame:CGRectMake(0,0,320,44)];
self.tableView.tableHeaderView = headerView;
//  [self.view addSubview:self.tableView.tableHeaderView];
//  [headerView release];


NSLog(@"Header: %@",self.tableView.tableHeaderView); //Logs ] Header:     <StandardTableHeaderView: 0x5a508b0; frame = (0 0; 320 44); layer = <CALayer: 0x5a51130>>

Edit: StandardTableHeaderView.m init method:

- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
    self.backgroundColor = [UIColor redColor];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(frame.origin.x,0,frame.size.width,frame.size.height)];
    self.label.backgroundColor = [UIColor clearColor];
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont fontWithName:@"Helvetica" size:16];
    [self addSubview:self.label];
}
return self;
}


Note! If you expect the table view to set the frame of the header view for you you're mistaken and will get very weird results.

I had mine set to CGRectZero thinking it'd solve itself, but the thing wouldn't appear and when it did, it would appear tucked in under the navbar or sticking down into the first cell and such.


First of all your code looks fine.

2 possible problems:

  • self.tableView is not set
  • tableHeaderView is overridden after viewDidLoad


I was having the same problem and noticed that any tableViewHeader that I defined in my NIB would not appear if I initialized my tableview by invoking:

- (id)initWithStyle:(UITableViewStyle)style

But it magically appeared without effort when I initialized my tableview by using:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

Weird.


I tried to put the setup of tableHeaderView in ViewDidLoad and loadView methods - it didn't work: part of the view was white, not visible completely. Then I tried to put it to ViewDidAppear and it worked:

- (void)viewDidAppear:(BOOL)animated
{
  [super viewDidAppear:animated];

  [self setupTableHeaderView];
}


Just came across the same problem where I had it in viewDidLoad() it worked in the simulator for iPhone 11 Pro but not for iPhone 8. Moving it to viewDidAppear() solved the problem, but I ended up moving it to viewDidLayoutSubviews() which I felt was the most appropriate place for it - works perfectly in the simulator across all iPhone models.

override func viewDidLayoutSubviews() {

    let headerView = Bundle.main.loadNibNamed("HeaderView", owner: self, options: nil)?.first as? UIView
    headerView!.frame = CGRect(x: 0, y: 0, width: table_view!.frame.width, height: 160.0)
    tableView!.tableHeaderView = headerView
}


I had a problem where i was putting a view inside a tableviewcontroller as a header and it would not show up when i ran the app, the problem was i added the header when there were no prototype cells. if you add at least one prototype cell, then add the header above it, then delete the cell (if need be), it registered it as a headerview properly


when you UIView inside the UITableView in the storyboard and you try to set tableView.tableHeaderView = YOUR_VIEW, it won't work. You have to first drag-drop that view outside view controller create its outlet and then set

tableView.tableHeaderView = YOUR_VIEW


Drawing upon the key clue from Kalle I've added this

open var intrinsicContentheight: CGFloat
{
    achievementLabel.sizeToFit()
    let labelSize = achievementLabel.bounds.size
    return labelSize.height + 13 + .verticalMargin
}

abomination to the class that defines the header view. If you don't have dynamic type you'd have a simpler method yet.

Here is the usage in the view controller that sets up the tableheaderview with the instance of this class

loanApplicationStepsView.bounds.size.height = loanApplicationStepsView.intrinsicContentheight

cause as Kalle have stated just because there is no func tableViewHeightForHeader(_ tableView: UITableView) -> CGFloat in uitableviewdelegate doesn't mean you don't have to do the dance to compute it

the width get set automatically by uikit


Try setting UITableViewDelegate in your header and place the following code in your .m file.

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 44.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    StandardTableHeaderView *headerView = [[StandardTableHeaderView alloc] initWithFrame:CGRectMake(0,0,320,44)];

    return headerView;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜