0

I'm calling setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine on a UITableView instance. The separator line should cover completely the width of table but instead it starts with some inset from the left. How can I have the separator line span the entire width of the table?

Sandy Chapman
  • 11,133
  • 3
  • 58
  • 67

3 Answers3

2

Please use below code which work for me in iOS 7 & 8 both version -

-(void)viewDidLayoutSubviews
{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
Santu C
  • 2,644
  • 2
  • 12
  • 20
1

With this codes it will be ok :

In method - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:

cell.layoutMargins = UIEdgeInsetsZero;
cell.preservesSuperviewLayoutMargins = NO;

EDIT : Changed code lines according to this post : iOS 8 UITableView separator inset 0 not working

Community
  • 1
  • 1
Jonathan
  • 606
  • 5
  • 19
  • You can also use just this two lines of code : cell.layoutMargins = UIEdgeInsetsZero; cell.preservesSuperviewLayoutMargins = NO;. I will edit my answer. – Jonathan Jun 15 '15 at 07:12
0

This code works on 7.0 and after.

You should set in your table view the desired inset:

if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
{
    [self.tableView setSeparatorInset:UIEdgeInsetsZero];
}

if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
{
    [self.tableView setLayoutMargins:UIEdgeInsetsZero];
}

Also you configure your cells like this:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];

    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
    {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
    {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }

    return cell;
}
Otávio
  • 735
  • 11
  • 23