How to hide first section header in UITableView (grouped style)

前端 未结 15 796
[愿得一人]
[愿得一人] 2020-12-07 11:15

As the design of table views using the grouped style changed considerably with iOS 7, I would like to hide (or remove) the first section header. So far I haven\'t managed to

相关标签:
15条回答
  • 2020-12-07 11:43

    I have a workaround that seems reasonably clean to me. So I'm answering my own question.

    Since 0 as the first section header's height doesn't work, I return 1. Then I use the contentInset to hide that height underneath the navigation bar.

    Objective-C:

    - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == 0)
                return 1.0f;
        return 32.0f;
    }
    
    - (NSString*) tableView:(UITableView *) tableView titleForHeaderInSection:(NSInteger)section
    {
        if (section == 0) {
            return nil;
        } else {
            // return some string here ...
        }
    }
    
    - (void) viewDidLoad
    {
        [super viewDidLoad];
    
         self.tableView.contentInset = UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0);
    }
    

    Swift:

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return section == 0 ? 1.0 : 32
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        tableView.contentInset = UIEdgeInsets(top: -1, left: 0, bottom: 0, right: 0)
    }
    
    0 讨论(0)
  • 2020-12-07 11:43

    easiest by far is to return nil, or "" in func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? for a section where you do not wish to display header.

    0 讨论(0)
  • 2020-12-07 11:45

    Here is how to get rid of the top section header in a grouped UITableView, in Swift:

    tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
    
    0 讨论(0)
  • 2020-12-07 11:45

    Swift Version: Swift 5.1
    Mostly, you can set height in tableView delegate like this:

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
       return UIView(frame: CGRect(x: 0, y: 0, width: view.width, height: CGFloat.leastNormalMagnitude))
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
       return CGFloat.leastNormalMagnitude
    }
    

    Sometimes when you created UITableView with Xib or Storyboard, the answer of up does not work. you can try the second solution:

    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: CGFloat.leastNormalMagnitude))
    self.tableView.tableHeaderView = headerView
    

    Hope it works for you!

    0 讨论(0)
  • 2020-12-07 11:46

    The answer was very funny for me and my team, and worked like a charm

    • In the Interface Builder, Just move the tableview under another view in the view hierarchy.

    REASON:

    We observed that this happens only for the First View in the View Hierarchy, if this first view is a UITableView. So, all other similar UITableViews do not have this annoying section, except the first. We Tried moving the UITableView out of the first place in the view hierarchy, and everything was working as expected.

    0 讨论(0)
  • 2020-12-07 11:49

    Use this trick for grouped type tableView

    Copy paste below code for your table view in viewDidLoad method:

    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, tableView.bounds.size.width, 0.01f)];
    
    0 讨论(0)
提交回复
热议问题