Is it possible to disable floating headers in UITableView with UITableViewStylePlain?

前端 未结 29 1108
走了就别回头了
走了就别回头了 2020-11-29 15:02

I\'m using a UITableView to layout content \'pages\'. I\'m using the headers of the table view to layout certain images etc. and I\'d prefer it if they didn\'t

29条回答
  •  情书的邮戳
    2020-11-29 15:15

    This can be achieved by assigning the header view manually in the UITableViewController's viewDidLoad method instead of using the delegate's viewForHeaderInSection and heightForHeaderInSection. For example in your subclass of UITableViewController, you can do something like this:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
        [headerView setBackgroundColor:[UIColor magentaColor]];
        [headerView setTextAlignment:NSTextAlignmentCenter];
        [headerView setText:@"Hello World"];
        [[self tableView] setTableHeaderView:headerView];
    }
    

    The header view will then disappear when the user scrolls. I don't know why this works like this, but it seems to achieve what you're looking to do.

提交回复
热议问题