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

前端 未结 15 795
[愿得一人]
[愿得一人] 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:28

    this way is OK.

    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0 {
            return CGFloat.min
        }
        return 25
    }
    
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0 {
            return nil
        }else {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-07 11:29

    Update[9/19/17]: Old answer doesn't work for me anymore in iOS 11. Thanks Apple. The following did:

    self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedSectionHeaderHeight = 20.0f;
    self.tableView.contentInset = UIEdgeInsetsMake(-18.0, 0.0f, 0.0f, 0.0);
    

    Previous Answer:

    As posted in the comments by Chris Ostomo the following worked for me:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        return CGFLOAT_MIN; // to get rid of empty section header
    }
    
    0 讨论(0)
  • 2020-12-07 11:35

    I just copied your code and tried. It runs normally (tried in simulator). I attached result view. You want such view, right? Or I misunderstood your problem?

    enter image description here

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

    In Swift 4.2 and many earlier versions, instead of setting the first header's height to 0 like in the other answers, you can just set the other headers to nil. Say you have two sections and only want the second one (i.e., 1) to have a header. That header will have the text Foobar:

    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return section == 1 ? "Foobar" : nil
    }
    
    0 讨论(0)
  • 2020-12-07 11:39

    Swift3 : heightForHeaderInSection works with 0, you just have to make sure header is set to clipsToBounds.

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
           return 0
    }
    

    if you don't set clipsToBounds hidden header will be visible when scrolling.

    func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
        guard let header = view as? UITableViewHeaderFooterView else { return }
    
        header.clipsToBounds = true
    }
    
    0 讨论(0)
  • 2020-12-07 11:40

    This is how to hide the first section header in UITableView (grouped style).

    Swift 3.0 & Xcode 8.0 Solution

    1. The TableView's delegate should implement the heightForHeaderInSection method

    2. Within the heightForHeaderInSection method, return the least positive number. (not zero!)

      func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
      
          let headerHeight: CGFloat
      
          switch section {
          case 0:
              // hide the header
              headerHeight = CGFloat.leastNonzeroMagnitude
          default:
              headerHeight = 21
          }
      
          return headerHeight
      }
      
    0 讨论(0)
提交回复
热议问题