Creating a Custom UITableView Separator Line

I would like to create a dividing line like this:

enter image description here

Any idea on how to implement it? I tried to get a line image and use the proxy UIAppearance objects:

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

but somehow only a black line is obtained.

+7
source share
3 answers

you can try:

 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; } 
+9
source

@Tarek I used two instances of your objects to create a 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 nice! Kudos to you.

+9
source

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) } } 
+1
source

All Articles