如果 UITableViewCell 为空,则隐藏删除分隔符行

我有一个 UITableView,我只有3行,我可以看到这3行。问题是单元格是空的: 我可以看到那里的线条。我不想看到那些线条。

知道怎么去掉这些线吗?

下面是我正在寻找的图像。

Image Link

62159 次浏览

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.

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];

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()

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];
}
tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

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;
}

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)

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

I guess this is what you are looking for.

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 1.0f;
}

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.

Updated answer for swift & iOS 9. This works for tableviews of the .Plain variety.

 tableView.tableFooterView = UIView()

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)
}

Just returning an empty UIView() in viewForFooterInSection worked for me:

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView()
}

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)

Swift Solution: tableView.tableFooterView = UIView(frame: CGRectZero)

Worked on Xcode 7.2