Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create custom UITableView separator line

I would like to create a separator line like this one:

enter image description here

Any idea about how to implement it? I tried getting an image of the line and using UIAppearance proxy objects:

[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorColor:
[UIColor colorWithPatternImage:[UIImage imageNamed:@"line.png"]]];
[[UITableView appearanceWhenContainedIn:[MyController class], nil] setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];

but,somehow, only the black line gets rendered.

like image 642
Claus Avatar asked Jun 28 '13 14:06

Claus


3 Answers

you can try below:

UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
separator.backgroundColor = myColor;
[cell.contentView addSubview:separator];

or

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"separator.png"]];
   imageView.frame = CGRectMake(0, 100, 320, 1);
   [customCell.contentView addSubview:imageView];

   return customCell;
}
like image 132
Tarek Hallak Avatar answered Oct 03 '22 07:10

Tarek Hallak


@Tarek I used two instance of your objects for creating the double line

UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 0.5, cell.contentView.frame.size.width, 1)];
UIView *separator2 = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height - 1.0, cell.contentView.frame.size.width, 1)];
separator.backgroundColor = [UIColor darkGrayColor];
separator2.backgroundColor = [UIColor blackColor];
[cell.contentView addSubview:separator];
[cell.contentView addSubview:separator2];

Looks good! Kudos for you

like image 31
Claus Avatar answered Sep 30 '22 07:09

Claus


Swift 3

viewDidLoad:

//remove default separator line
tableView.separatorStyle = .none

tableView cell:

class MyCustomCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()

        let separator = UIView(frame: CGRect(x: 8, y: bounds.size.height - 0.5, width: bounds.size.width - 22, height: 1))
        separator.backgroundColor = UIColor.red
        contentView.addSubview(separator)
    }
}
like image 35
Peter Kreinz Avatar answered Oct 01 '22 07:10

Peter Kreinz