1

I followed this post: How to customize tableView separator in iPhone

Problem is that it doesn't work well when I have custom height for my cell.

I'll show you with two images, the one with two lines is the result of having a custom height for my cells.

enter image description here

With custom height: enter image description here

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];

    lineView.backgroundColor = [UIColor darkGrayColor];
    [cell.contentView addSubview:lineView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
Community
  • 1
  • 1
  • Is the custom height just for one cell or all cells? – soulshined Dec 10 '14 at 16:32
  • Do you have a custom cell ? In a nib? The problem is that the cell is expanded after you set your lineView. If you want it to stick to the bottom, you can check out the autoresizingMask or set up autoLayout constraints – streem Dec 10 '14 at 16:37

1 Answers1

0

Hi the problem is in the cellForRowAtIndexPath methods you cell doesn't know its height yet. If you use a custom height (you know this height, use it in the cellForRow... also).

Here example:

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 49.0, cell.contentView.frame.size.width, 1)];

lineView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:lineView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}

Also, don´t forget you can use separatorStyle and separatorInset, in order to custom a litle this line. If you don't use it put to none:

   self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Onik IV
  • 5,007
  • 2
  • 18
  • 23
  • This looks promising, I will test it when I am back in the office, thanks! –  Dec 10 '14 at 17:03
  • 1
    Onik's answer is right, just one thing - better, if you not hardcode layout, but override UITableViewCell layoutSubviews function and put there something like `self.lineView.frame = CGRectMake(0, self.frame.size.height - 1, self.frame.size.width, 1);` – Vitalii Gozhenko Dec 10 '14 at 18:00