Is it possible to have a fixed uitableview Header while using sections?

后端 未结 4 1430
天涯浪人
天涯浪人 2020-12-05 09:38

This question should not be mixed up with this here.. These are two different things.

There is a good example how to use a UITableView Header on SO.

This all

相关标签:
4条回答
  • 2020-12-05 10:09

    There’s no way to maintain the header of a tableView fixed, but an useful approach when you need a unique header, is to use a UIViewController rather than a UITableViewController, and set the header (UIView) out from the tableView.

    Something like this:

    enter image description here

    0 讨论(0)
  • 2020-12-05 10:16

    If you want a UITableViewController (static cells/keyboard handling) and have a fixed header then you should use Containment. You can do this from a Storyboard by setting up a UIViewController with your fixed header and then using a Container View to embed the UITableViewController.

    Once you have your containing view setup, you right-click drag from the Container View to the View Controller you want to embed - the UITableViewController in this case.

    You can access and get a reference to the contained View Controller (the UITableViewController) from the Container View Controller by implementing the prepareForSegue:sender: method.

    0 讨论(0)
  • 2020-12-05 10:26

    If you want to keep the class as a UITableViewController you can add your header as a subview to the tableview's superview. You will have to also push the tableview top inset down so your headerview doesnt hide the table.

    Here is a sample code to put inside your tableViewController subclass (This example assumes your tableview controller is inside a navigation controller, so it pushes the view to below the navigation bar):

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        self.automaticallyAdjustsScrollViewInsets = NO;
    
    }
    
    -(void)addHeaderView{
        CGFloat yPosition = self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height;
        mainHeaderView = [[UIView alloc] init];
    
        const CGFloat mainHeaderHeight = 44;
        [mainHeaderView setFrame:CGRectMake(0, yPosition, self.view.frame.size.width, mainHeaderHeight)];
    
        mainHeaderView.backgroundColor = [UIColor redColor];
    
        [self.tableView.superview addSubview:mainHeaderView];
        [self.tableView setContentInset:UIEdgeInsetsMake(yPosition + mainHeaderHeight, self.tableView.contentInset.left, self.tableView.contentInset.bottom, self.tableView.contentInset.right)];
     }
    
    0 讨论(0)
  • 2020-12-05 10:27

    I haven't done this, but the first thing I would think to try is to place my tableview in a UIView and make my own header there in that UIView. Seems a trivial matter to make that view appear to be the header of the table and it would certainly stay put.

    0 讨论(0)
提交回复
热议问题