Programmatically remove the header of UITableView and automatically resizes the content to fill in the removed area

前端 未结 4 1155
渐次进展
渐次进展 2020-12-17 10:32

I have added a UIButton in the header section of the UITableView via the interface builder and would like to remove the access to the button in cer

相关标签:
4条回答
  • 2020-12-17 10:38

    Check if your tableView has a grouped style, in that case a white space appears at the top of the table. Change the style to plain and that blank space will be removed.

    0 讨论(0)
  • 2020-12-17 10:51

    I created a boolean property called removeHeader and then when ever I want to remove my header I call:

    func removeSectionHeader() {
            removeHeader = true
            self.tableView.reloadData()
        }
    

    Then when my tableView reloads it will call this tableView delegate method:

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            if removeHeader {
                return 0.0
            } else {
                let height = self.tableView.sectionHeaderHeight
                return height
            }
        }
    
    0 讨论(0)
  • 2020-12-17 10:58

    If you want to remove the table's header view, just set the myTable.tableHeaderView property to nil. If what you have is actually a section header, then you need to return nil from the viewForHeaderInSection method and call [myTableView reloadData]

    0 讨论(0)
  • 2020-12-17 10:59

    You could also do:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 0.0;
    }
    

    This seems to work for my (single) section header and avoids the need for reloadData.

    Note that:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    

    is still called, but its return value seems to be ignored.

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