1

Is there a way to set separator inset for a single UITableViewCell? It seems I can only set global UITableView separator inset with setSeparatorInset: method, but I would like to have different insets in the same UITableView. Is it possible somehow?

I tried using tableView:willDisplayCell:forRowAtIndexPath: for setting separator inset in a concrete UITableViewCell as below, but it does not work.

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
  ...
  [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)];
}

I am working on iOS7.

Pablo
  • 2,834
  • 5
  • 25
  • 45
  • try to write that in uitableview delegate method cellForRowAtIndexPath – Malav Soni Apr 28 '15 at 08:09
  • That won't work. Problem is `setSeparatorInset:` method exists in UITableView, so if I use it it will change separator inset for the whole UITableView, not for a concrete cell. – Pablo Apr 28 '15 at 08:14
  • possible duplicate of [iOS 8 UITableView separator inset 0 not working](http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working) – GoodSp33d Apr 28 '15 at 08:21

2 Answers2

1

If you know the cell's indexPath. You can set it using

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { ... if (indexPath.row == 0) { // Customize here [cell setSeparatorInset:UIEdgeInsetsMake(0, 50, 0, 0)]; } else { [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; } }

If not, you are supposed to subclass UITableView and customize separator insets there. Then load it into your tableview. DO NOT set separatorInset property both in cellForRowAtIndexPath: method and subclasses.

Draven Zuo
  • 111
  • 2
  • 8
0

You can do it by using custom cells. You set your UITableView to not show separators at all (separatorStyle = UITableViewCellSeparatorStyleNone) and have your UITableViewCell subclass handle the drawing of the separators. The easiest way would be to have a "separator view" on each cell. Now when configuring your cells in cellForRowAtIndexPath you can set whatever inset that you like.

Artal
  • 8,933
  • 2
  • 27
  • 30