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]
What am I doing wrong?
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.
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).
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;
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With