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

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

    ref: https://stackoverflow.com/a/26306212

    let tableView = UITableView(frame: .zero, style: .grouped)

    PLUSE

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return 40
        }else{
            tableView.sectionHeaderHeight = 0
        }
    
        return 0
    }
    

    This helped to use header space

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

    Maybe you can simply make header view background transparent:

    - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
        view.tintColor = [UIColor clearColor];
    }
    

    Or apply it globally:

        [UITableViewHeaderFooterView appearance].tintColor = [UIColor clearColor];
    
    0 讨论(0)
  • 2020-11-29 15:40

    2020 Latest Update

    Tested with Xcode 14.

    For hiding any section header, Return nil for title of section delegate

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return nil
    }
    
    0 讨论(0)
  • 2020-11-29 15:41

    WARNING: this solution implements a reserved API method. This could prevent the app from being approved by Apple for distribution on the AppStore.

    I've described the private methods that turns of section headers floating in my blog

    Basically, you just need to subclass UITableView and return NO in two of its methods:

    - (BOOL)allowsHeaderViewsToFloat;
    - (BOOL)allowsFooterViewsToFloat;
    
    0 讨论(0)
  • 2020-11-29 15:41

    While thinking how to approach this problem, I remembered a very important detail about UITableViewStyleGrouped. The way UITableView implements the grouped style (the rounded borders around the cells) is by adding a custom backgroundView to the UITableViewCells, and not to the UITableView. Each cell is added a backgroundView according to its position in the section (upper rows get the upper part of the section border, middle ones get the side border and the bottom one gets – well, the bottom part). So, if we just want a plain style, and we don’t have a custom backgroundView for our cells (which is the case in 90% of the times), then all we need to do is use UITableViewStyleGrouped, and remove the custom background. This can be done by following those two steps:

    Change our tableView style to UITableViewStyleGrouped Add the following line to cellForRow, just before we return the cell:

    cell.backgroundView=[[[UIView alloc] initWithFrame:cell.bounds] autorelease];

    And that’s it. The tableView style will become exactly like UITableViewStylePlain, except for the floating headers.

    Hope this helps!

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