Grouped UITableView shows blank space when section is empty

后端 未结 11 586
别跟我提以往
别跟我提以往 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:22

    I have a similar situation and my data model really works better if I can let it have empty sections.

    Your problem can be solved if you set self.tableView.sectionHeaderHeight = 0; and self.tableView.sectionFooterHeight = 0; in your tableview controller. These must be 0 because the delegate methods somehow ignore a return value of 0. Then you have to override some delegate methods:

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        if (section==0)
            return sectionGapHeight;
        if ([self tableView:tableView numberOfRowsInSection:section]==0) {
            return 0;
        }
        return sectionGapHeight;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
        if (section==[self numberOfSectionsInTableView:tableView]-1) {
            return sectionGapHeight;
        }
        return 0;
    }
    - (UIView *)sectionFiller {
        static UILabel *emptyLabel = nil;
        if (!emptyLabel) {
            emptyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            emptyLabel.backgroundColor = [UIColor clearColor];
        }
        return emptyLabel;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        return [self sectionFiller];
    }
    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        return [self sectionFiller];
    }
    

    This code will also make the gap between sections the same height as the gap before the first section and the gap below the last. That looks better in my opinion than the default where the gap between is twice as big as the ones at the top and the bottom.

提交回复
热议问题