views:

198

answers:

3

Hello,

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 records that don't have certain sections show up as blank spaces in the table (see picture)

alt text

There are no footers/headers. Anything I've missed?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self getRowCount:section];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [NSString stringWithFormat:@"section: %d row: %d",indexPath.section,indexPath.row];
// Configure the cell...

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    float height = 0.0;

    if([self getRowCount:indexPath.section] != 0){
        height = kDefaultRowHeight;
    }

    return height;

}
+2  A: 

In the above screenshot, is your numberOfSectionsInTableView: method returning a value of 5? And then is your getRowCount: method (called from numberOfRowsInSection:) returning a value of 0 for those "missing" sections (e.g. 1, 3 and 4)?

If you don't want gaps in between, the likely solution is to only declare the number of sections you want to be visible. In your example above, you'd only want to return value of 3 from numberOfSectionsInTableView:.

Shaggy Frog
A: 

I believe that with section table views, each section must exist, and each section has buffer room for a header and a footer regardless of you putting text in them. What you need to do is remove the sections that aren't being used. In your example, you would remove section 1, 3, and 4. This would mean:

"section 0" is indexPath.section == 0

"section 2" is indexPath.section == 1

"section 5" is indexPath.section == 2

If you code in this fashion, the headers and footers will be removed because the nil cell is removed.

This isn't proper coding of course but I just wanted to give you an idea of the theology. Hope this helps.

DysonApps
A: 

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.