Hide remove separator line if UITableViewCells are empty
IosUitableviewCocoa TouchIos Problem Overview
I have a UITableView
and I have only 3 rows in it, and I can see those 3 rows. The problem is the cells that are empty: I can see lines there. I don't want to see those lines.
Any idea how to remove those lines?
Below is image for what I am looking for.
Ios Solutions
Solution 1 - Ios
Even simpler than Andrey Z's reply: Simply make a bogus tableFooterView in your UITableView class:
self.tableFooterView = [UIView new]; // to hide empty cells
and Swift:
tableFooterView = UIView()
Solution 2 - Ios
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];
OR
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];
OR
- (float)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
// To "clear" the footer view
return [[UIView new] autorelease];
}
OR
And also check nickfalk's answer, it is very short and helpful too. And you should also try this single line,
self.tableView.tableFooterView = [[UIView alloc] init];
Not sure but it's working in all the version of iOS that I checked, with iOS 5 and later, up to iOS 7.
Solution 3 - Ios
Updated answer for swift & iOS 9. This works for tableviews of the .Plain
variety.
tableView.tableFooterView = UIView()
Solution 4 - Ios
Transparent UIView
as a tableView
footer with 1px height will do the trick.
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];
Solution 5 - Ios
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Solution 6 - Ios
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];
}
Solution 7 - Ios
Just returning an empty UIView()
in viewForFooterInSection
worked for me:
override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}
Solution 8 - Ios
Please try the following code:
self.tblView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
if ([self.tblView respondsToSelector:@selector(setSeparatorInset:)])
{
[self.tblView setSeparatorInset:UIEdgeInsetsZero];
}
// write in view did load
Solution 9 - Ios
tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Solution 10 - Ios
None of suggested answers were suitable for my similar problem. Implementing this method in tableView delegate finally worked:
- (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
Solution 11 - Ios
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)
Solution 12 - Ios
I guess this is what you are looking for.
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1.0f;
}
Solution 13 - Ios
You can set the table view to be grouped instead of plain - this changes the look a bit but at least removes the extra lines.
I had this exact problem and ended up changing to grouped views. Looks a lot better.
Solution 14 - Ios
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:
- ask for a subview for that tag let divider = cell.viewWithTag(TAG) ...
- if present, do NOT add another subview
- 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)
}
Solution 15 - Ios
Inside the cellForRowAtIndexPath
let separatorLineView:UIView = UIView(frame: CGRectMake(0,0,self.tableview.bounds.width,0.5))
separatorLineView.backgroundColor = tableView.separatorColor
cell!.contentView.addSubview(separatorLineView)
Solution 16 - Ios
Swift Solution: tableView.tableFooterView = UIView(frame: CGRectZero)
Worked on Xcode 7.2