ios 如果 UITableViewCells 为空,则隐藏删除分隔线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14460772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:57:44  来源:igfitidea点击:

Hide remove separator line if UITableViewCells are empty

iosuitableviewcocoa-touch

提问by Fahim Parkar

I have a UITableViewand 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.

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

Any idea how to remove those lines?

知道如何删除这些行吗?

Below is image for what I am looking for.

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

Image Link

图片链接

回答by T. Benjamin Larsen

Even simpler than Andrey Z's reply: Simply make a bogus tableFooterView in your UITableView class:

甚至比 Andrey Z 的回复更简单:只需在 UITableView 类中创建一个伪造的 tableFooterView:

self.tableFooterView = [UIView new]; // to hide empty cells

and Swift:

和斯威夫特:

tableFooterView = UIView()

tableFooterView = UIView()

回答by iPatel

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 UIViewof 1px height:

您可以UITableView使用以下任一代码片段来隐藏的标准分隔线。添加自定义分隔符的最简单方法是添加一个简单UIView的 1px 高度:

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,

并检查nickfalk's answer,它也非常简短且有帮助。你也应该试试这一行,

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.

不确定,但它适用于我检查过的所有 iOS 版本,iOS 5 及更高版本,直到 iOS 7。

回答by seo

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

更新了 swift 和 iOS 9 的答案。这适用于.Plain各种表格视图。

 tableView.tableFooterView = UIView()

回答by Andrey Zverev

Transparent UIViewas a tableViewfooter with 1px height will do the trick.

UIView作为tableView具有 1px 高度的页脚透明即可。

UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 1)];
v.backgroundColor = [UIColor clearColor];
[self.tableView setTableFooterView:v];

回答by Robert Varga

self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

回答by Pradhyuman sinh

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

回答by Aatish Javiya

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

回答by codelearner

Just returning an empty UIView()in viewForFooterInSectionworked for me:

只是返回一个空UIView()viewForFooterInSection对我有用:

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

回答by Mak083

You can use

您可以使用

tableForBrands.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

It worked for me...

它对我有用...

回答by Deepak Badiger

I guess this is what you are looking for.

我想这就是你要找的。

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