Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separation between Header and first cell -- In plain UITableView

I have a table with with Plain style and only one section. I have a implemented viewForHeaderInSection: to add a custom view in the section header.

I am not able to see a separation line between my table section header view and my first cell. [See the attached image]

alt text

What am I doing wrong?

like image 420
Abhinav Avatar asked Nov 29 '10 21:11

Abhinav


4 Answers

As Jeremy notes in his answer, iOS doesn't add separators above/below the headers/footers; you can just use a UIView to create a line yourself.

Here's the code to add a standard looking separator view to a header view:

CGRect sepFrame = CGRectMake(0, headerView.frame.size.height-1, 320, 1);
seperatorView = [[[UIView alloc] initWithFrame:sepFrame] autorelease];
seperatorView.backgroundColor = [UIColor colorWithWhite:224.0/255.0 alpha:1.0];
[headerView addSubview:seperatorView];

If you're trying to make it look like a normal table view cell, you probably need to add one at the top of the header view too.

like image 79
JosephH Avatar answered Nov 03 '22 11:11

JosephH


Custom headers and footers do not contain separators below/above them. You'll need to implement the separator yourself in the custom view (or switch to grouped style, which will show the outline of the group above and below it even with a custom header/footer).

like image 26
Jeremy Fuller Avatar answered Nov 03 '22 12:11

Jeremy Fuller


if you want to give space only between table header and table first row then you can use

In method of tableView:heightForHeaderInSection:(NSInteger)section

if(section ==0)
    return 3; // (space u want to give between header and first row);

return 10; //(ur section header height)

In method of tableView:viewForHeaderInSection:(NSInteger)section

  UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 3)];
  headerView.backgroundColor = [UIColor clearColor];   // use your own design

  return headerView;
like image 11
Usman Nisar Avatar answered Nov 03 '22 11:11

Usman Nisar


Add an extra "hidden" row to the section you want to add the separator to by returning +1 the existing number of rows in tableView:numberOfRowsInSection:. Then add the following method:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ( indexPath.section == sectionOfHiddenRow && indexPath.row == indexOfHiddenRow )
        return 0.f;
    else
        return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}

If you want the separator at the top of a section (after a header), indexOfHiddenRow will be 0. If you want it at the bottom of a section (before a footer) it will be [self tableView:tableView numberOfRowsInSection:sectionOfHiddenRow] - 1.

Now inside tableView:cellForRowAtIndexPath:, simply return [UITableViewCell new] for the hidden row (it won't be displayed, so no need to set a frame or anything). You may need to do some -1 index adjustments in your UITableViewDataSource and UITableViewDelegate methods, but it works (tested in iOS 7), and guarantees consistent styling (no need to draw your own "fake" separator—this is a true system-drawn UITableView separator).

like image 5
devios1 Avatar answered Nov 03 '22 10:11

devios1