Senseful
Senseful

Reputation: 91931

Show table view separator for empty sections

I have a UITableView set to the UITableViewStyleGrouped style. Whenever a section has one or more items, it displays a one pixel separator beneath the section header. However, if the section has no items, there is no separator between sections.

How can I get the separator to appear in all sections, even those that have no cells?

enter image description here

Notice how section 1 and 2 have a separator between them and their first cell, but section 3 doesn't have a separator between it and section 4.

Upvotes: 0

Views: 446

Answers (3)

Senseful
Senseful

Reputation: 91931

One solution is to add a footer to each empty section:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return [self tableView:tableView numberOfRowsInSection:section] == 0 ? 0.5 : 0.00000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if ([self tableView:tableView numberOfRowsInSection:section] == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0.5)];
        view.backgroundColor = tableView.separatorColor;
        return view;
    } else {
        return nil;
    }
}

Notes:

This produces correct results most of the time:

enter image description here

... but has several problems:

  1. This exploits the footer, so if you need to display a footer, this solution probably won't work.
  2. In edit mode, when you drag the last item out of a section, the border will disappear between that section and the one below it.
  3. In edit mode, when you drag an item into an empty section, there will be a double border at the bottom of the section.

Example of problems 2 and 3:

enter image description here

In the above image, notice how there is no line between sections 2 and 3, since all the items were moved out of that section (problem 2).

Also notice how the last items in section 3 and 4 have double borders, since they were dragged into new sections (problem 3).

Upvotes: 1

iBhavin
iBhavin

Reputation: 1261

Use this two method to add separator between Section.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44.0;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];

    [headerView setBackgroundColor:[UIColor brownColor]];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
                    img.backgroundColor = [UIColor whiteColor];

    [headerView addSubview:img];
    return headerView;
}

Upvotes: 0

ShineWang
ShineWang

Reputation: 174

You can try customize your section header view. use this delegate funciton: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

Upvotes: 0

Related Questions