Hide separator line on one UITableViewCell

Safari picture Safari · Dec 19, 2011 · Viewed 243k times · Source

I'm customizing a UITableView. I want to hide the line separating on the last cell ... can i do this?

I know I can do tableView.separatorStyle = UITableViewCellStyle.None but that would affect all the cells of the tableView. I want it to only affect my last cell.

Answer

Hiren picture Hiren · Dec 19, 2011

in viewDidLoad, add this line:

self.tableView.separatorColor = [UIColor clearColor];

and in cellForRowAtIndexPath:

for iOS lower versions

if(indexPath.row != self.newCarArray.count-1){
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 44, 320, 2)];
    line.backgroundColor = [UIColor redColor];
    [cell addSubview:line];
}

for iOS 7 upper versions (including iOS 8)

if (indexPath.row == self.newCarArray.count-1) {
    cell.separatorInset = UIEdgeInsetsMake(0.f, cell.bounds.size.width, 0.f, 0.f);
}