You can hide UITableView's standard separator line by using any one of the below snippets of code.
The easiest way to add a custom separator is to add a simple UIView of 1px height:
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
separatorLineView.backgroundColor = [UIColor clearColor]; // set color as you want.
[cell.contentView addSubview:separatorLineView];
Use this Code for remove separator line for empty cells.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
If you are using Storyboards you can just drag and drop an UIView into your UITableView below your cells and set its height to 0. (Have only tested in an iOS 8 project)
Some of previous suggestions contain a BIG conceptual error:
if You do:
[cell addSubview: ....
even time a cell is "reused", you will add a new subview for the divider!
avoid it in two ways:
a) use a TAG, and:
1) ask for a subview for that tag
let divider = cell.viewWithTag(TAG) ...
2) if present, do NOT add another subview
3) if NOT present add AND tag it.
b) create a custom view and ADD your custom divider in "init" "awakeFromNib" of custom cell.
code for a):
if let divider = cell.viewWithTag(DIVIDER_TAG) as? UIView{
// nothing.. eventually change color bases in IndexPath...
}else{
let frame = CGRectMake(0, cell.frame.height-1, cell.frame.width, 1)
divider.tag = DIVIDER_TAG
divider.backgroundColor = UIColor.redColor()
cell.addSubview(divider)
}