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

前端 未结 29 990
走了就别回头了
走了就别回头了 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:20

    To remove the floating section header sections completely, you can do this:

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
    {
        return [[UIView alloc] init];
    }
    

    return nil doesn't work.

    To disable floating but still show section headers you can provide a custom view with its own behaviours.

    0 讨论(0)
  • 2020-11-29 15:21

    Change your TableView Style:

    self.tableview = [[UITableView alloc] initwithFrame:frame style:UITableViewStyleGrouped];
    

    As per apple documentation for UITableView:

    UITableViewStylePlain- A plain table view. Any section headers or footers are displayed as inline separators and float when the table view is scrolled.

    UITableViewStyleGrouped- A table view whose sections present distinct groups of rows. The section headers and footers do not float.

    0 讨论(0)
  • 2020-11-29 15:22

    Another way to do it is to make an empty section right before the one you want the header on and put your header on that section. Because the section is empty the header will scroll immediately.

    0 讨论(0)
  • 2020-11-29 15:24

    In your Interface Builder click on your problem Table View

    Then navigate to Attributes Inspector and change Style Plain to Grouped ;) Easy

    0 讨论(0)
  • 2020-11-29 15:27

    There is another tricky way. The main idea is to double the section number, and first one only shows the headerView while the second one shows the real cells.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return sectionCount * 2;
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        if (section%2 == 0) {
            return 0;
        }
        return _rowCount;
    }
    

    What need to do then is to implement the headerInSection delegates:

    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        if (section%2 == 0) {
            //return headerview;
        }
        return nil;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        if (section%2 == 0) {
            //return headerheight;
        }
        return 0;
    }
    

    This approach also has little impact on your datasources:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        int real_section = (int)indexPath.section / 2;
        //your code
    }
    

    Comparing with other approaches, this way is safe while not changing the frame or contentInsets of the tableview. Hope this may help.

    0 讨论(0)
  • 2020-11-29 15:27

    You can add one Section(with zero rows) above, then set the above sectionFooterView as current section's headerView, footerView doesn't float. Hope it gives a help.

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