ios 我可以强制 UITableView 隐藏空单元格之间的分隔符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1633966/
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 16:33:48  来源:igfitidea点击:

Can I force a UITableView to hide the separator between empty cells?

iosobjective-ccocoa-touchuitableview

提问by jessecurry

When using a plain-style UITableViewwith a large enough number of cells that the UITableViewcannot display them all without scrolling, no separators appear in the empty space below the cells. If I have only a few cells the empty space below them includes separators.

当使用具有UITableView足够多单元格的普通样式时,UITableView如果不滚动就无法显示所有单元格,则单元格下方的空白区域不会出现分隔符。如果我只有几个单元格,它们下方的空白区域包括分隔符。

Is there a way that I can force a UITableViewto remove the separators in the empty space? If not I'll have to load a custom background with a separator drawn in for each cell which will make it harder to inherit behavior.

有没有办法可以强制 aUITableView删除空白空间中的分隔符?如果不是,我将不得不加载一个自定义背景,并为每个单元格绘制一个分隔符,这将使继承行为变得更加困难。

I found a somewhat similar question here, but I can't use a grouped UITableViewin my implementation.

我在这里发现了一个有点类似的问题,但我不能UITableView在我的实现中使用分组。

采纳答案by Daniel Hepper

You can achieve what you want by defining a footer for the tableview. See this answer for more details:Eliminate Extra separators below UITableView

您可以通过为 tableview 定义页脚来实现您想要的。有关更多详细信息,请参阅此答案:消除 UITableView 下面的额外分隔符

回答by J. Costa

For iOS 7.* and iOS 6.1

对于 iOS 7.* 和 iOS 6.1

The easiest method is to set the tableFooterViewproperty:

最简单的方法是设置tableFooterView属性:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // This will remove extra separators from tableview
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

For previous versions

对于以前的版本

You could add this to your TableViewController (this will work for any number of sections):

您可以将其添加到您的 TableViewController(这适用于任意数量的部分):

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
     // This will create a "invisible" footer
     return 0.01f;
 }

and if it is not enough, add the following code too:

如果这还不够,添加以下代码

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{        
    return [UIView new];

    // If you are not using ARC:
    // return [[UIView new] autorelease];
}

回答by Sebastian

For Swift:

对于斯威夫特:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.tableFooterView = UIView()  // it's just 1 line, awesome!
}

回答by Sauleil

Using the link from Daniel, I made an extension to make it more usable:

使用 Daniel 的链接,我做了一个扩展以使其更有用:

//UITableViewController+Ext.m
- (void)hideEmptySeparators
{
    UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
    v.backgroundColor = [UIColor clearColor];
    [self.tableView setTableFooterView:v];
    [v release];
}

After some testings, I found out that the size can be 0 and it works as well. So it doesn't add some kind of margin at the end of the table. So thanks wkw for this hack. I decided to post that here since I don't like redirect.

经过一些测试,我发现大小可以为 0 并且它也可以工作。所以它不会在表格的末尾添加某种边距。所以感谢 wkw 的这次黑客攻击。我决定在这里发布,因为我不喜欢重定向。

回答by King-Wizard

SwiftVersion

迅捷

The easiest method is to set the tableFooterView property:

最简单的方法是设置 tableFooterView 属性:

override func viewDidLoad() {
    super.viewDidLoad()
    // This will remove extra separators from tableview
    self.tableView.tableFooterView = UIView(frame: CGRectZero)
}

回答by Segev

For Swift:

对于斯威夫特:

self.tableView.tableFooterView = UIView(frame: CGRectZero)

For newest Swift:

对于最新的 Swift:

self.tableView.tableFooterView = UIView(frame: CGRect.zero)

回答by Salmo

If you use iOS 7 SDK, this is very simple.

如果您使用 iOS 7 SDK,这很简单。

Just add this line in your viewDidLoad method:

只需在您的 viewDidLoad 方法中添加这一行:

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

回答by duncanwilcox

Setting the table's separatorStyleto UITableViewCellSeparatorStyleNone(in code or in IB) should do the trick.

将表格设置separatorStyleUITableViewCellSeparatorStyleNone(在代码中或在 IB 中)应该可以解决问题。

回答by Dare2Dream

I use the following:

我使用以下内容:

UIView *view = [[UIView alloc] init];
myTableView.tableFooterView = view;
[view release];

Doing it in viewDidLoad. But you can set it anywhere.

在 viewDidLoad 中进行。但是你可以在任何地方设置它。

回答by Callaghan001

The following worked very well for me for this problem:

对于这个问题,以下对我来说非常有效:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

CGRect frame = [self.view frame];
frame.size.height =  frame.size.height - (kTableRowHeight * numberOfRowsInTable);

UIView *footerView = [[UIView alloc] initWithFrame:frame];
return footerView; }

Where kTableRowHeight is the height of my row cells and numberOfRowsInTable is the number of rows I had in the table.

其中 kTableRowHeight 是我的行单元格的高度,numberOfRowsInTable 是我在表格中的行数。

Hope that helps,

希望有所帮助,

Brenton.

布伦顿。