How to hide a section in UITableView?

前端 未结 12 2218
梦如初夏
梦如初夏 2020-12-05 00:32

There are some section in the table that does not contain any data and would like to hide that section.

How to do this?

12条回答
  •  长情又很酷
    2020-12-05 01:13

    For hiding a section, even in the middle of the table view you would need all of the following

    #define DEBUG_SECTION 1
    
    #if ! DEBUG_BUILD
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
    {
        if (section == DEBUG_SECTION)
            return CGFLOAT_MIN;
        return [super tableView:tableView heightForHeaderInSection:section];
    }
    //------------------------------------------------------------------------------
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
    {
        if (section == DEBUG_SECTION)
            return CGFLOAT_MIN;
        return [super tableView:tableView heightForFooterInSection:section];
    }
    //------------------------------------------------------------------------------
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        if (section == DEBUG_SECTION)
            return nil;
        return [super tableView:tableView titleForHeaderInSection:section];
    }
    //------------------------------------------------------------------------------
    
    - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
    {
        if (section == DEBUG_SECTION)
            return nil;
        return [super tableView:tableView titleForFooterInSection:section];
    }
    //------------------------------------------------------------------------------
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        if (section == DEBUG_SECTION)
            return 0;
        return [super tableView:tableView numberOfRowsInSection:section];
    }
    //------------------------------------------------------------------------------
    #endif // #if ! DEBUG_BUILD
    

    if you left out titleFor/headerFor... you will get some empty rows
    if you left out heightFor... the title/footer header text will still appear

提交回复
热议问题