Grouped UITableView shows blank space when section is empty

后端 未结 11 601
别跟我提以往
别跟我提以往 2020-12-30 23:44

I have a grouped UITableView where not all sections may be displayed at once, the table is driven by some data that not every record may have. My trouble is that the record

11条回答
  •  没有蜡笔的小新
    2020-12-31 00:27

    Here is the most elegant solution I've found based on all the answers here, plus this answer:

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
        if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
            return nil;
        } else {
            // Return title normally
        }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        if ([self tableView: tableView numberOfRowsInSection: section] == 0) {
            return 0.01f;;
        }
    
        return UITableViewAutomaticDimension;
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        if ([self tableView: tableView numberOfRowsInSection: section] == 0) {
            return 0.01f;;
        }
    
        return UITableViewAutomaticDimension;
    }
    

提交回复
热议问题