ios 消除 UITableView 下面的额外分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1369831/
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
Eliminate extra separators below UITableView
提问by RoundOutTooSoon
回答by J. Costa
Interface builder (iOS 9+)
界面构建器(iOS 9+)
Just drag a UIView to the table. In storyboard, it will sit at the top below your custom cells. You may prefer to name it "footer".
只需将 UIView 拖到表格中即可。在故事板中,它将位于自定义单元格下方的顶部。您可能更愿意将其命名为“页脚”。
Here it is shown in green for clarity, you'd probably want clear color.
为清楚起见,此处显示为绿色,您可能需要清晰的颜色。
Note that by adjusting the height, you can affect how the "bottom bounce" of the table is handled, as you prefer. (Height zero is usually fine).
请注意,通过调整高度,您可以根据自己的喜好影响桌子“底部弹跳”的处理方式。(高度为零通常很好)。
To do it programmatically:
以编程方式执行此操作:
Swift
迅速
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.tableFooterView = UIView()
}
Objective-C
目标-C
iOS 6.1+
iOS 6.1+
- (void)viewDidLoad
{
[super viewDidLoad];
// This will remove extra separators from tableview
self.tableView.tableFooterView = [UIView new];
}
or if you prefer,
或者如果你愿意,
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
Historically in iOS:
历史上在 iOS 中:
Add to the table view controller...
添加到表视图控制器...
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
// This will create a "invisible" footer
return CGFLOAT_MIN;
}
and if necessary...
如果有必要……
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
return [UIView new];
// If you are not using ARC:
// return [[UIView new] autorelease];
}
回答by wkw
Here's another way to do that w/out the grouped table style, and one you'd probably not guess. Adding a header andfooter to the table (perhaps one or the other suffices, haven't checked)causes the separators to disappear from the filler/blank rows.
这是另一种不使用分组表格样式的方法,您可能不会猜到。向表格中添加页眉和页脚(也许其中一个就足够了,尚未检查)会导致分隔符从填充/空白行中消失。
I stumbled onto this because I wanted a little space at the top and bottom of tables to decrease the risk of hitting buttons instead of a table cell with meaty fingers. Here's a method to stick a blank view in as header and footer. Use whatever height you like, you still eliminate the extra separator lines.
我偶然发现了这一点,因为我希望在桌子的顶部和底部留出一点空间,以减少按按钮的风险,而不是手指多肉的桌子单元。这是一种将空白视图作为页眉和页脚粘贴的方法。使用任何你喜欢的高度,你仍然可以消除额外的分隔线。
- (void) addHeaderAndFooter
{
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];
v.backgroundColor = [UIColor clearColor];
[self.myTableView setTableHeaderView:v];
[self.myTableView setTableFooterView:v];
[v release];
}
In response to @Casebash, I went back to the code in my app ("AcmeLists" List Manager in iTunes store...) and short-circuited the addHeaderAndFooter method to verify. Without it, I have the extra row separators; with the code, I have what you see in this window snap: no table row separators picture. So I'm not sure why it wouldn't have worked for you. Moreover, it makes sense to me that having any custom footer on a table view would necessarily have to stop drawing row separators for blank rows below it. That would be hideous. For reference, I looked at tables where there were more rows than could be viewed on screen, and then for a table with two rows. In both cases, no extraneous separators.
作为对@Casebash 的回应,我回到了我的应用程序中的代码(iTunes 商店中的“AcmeLists”列表管理器...)并短路了 addHeaderAndFooter 方法以进行验证。没有它,我有额外的行分隔符;使用代码,我可以看到您在此窗口快照中看到的内容:没有表格行分隔符图片。所以我不确定为什么它对你不起作用。此外,对我来说,在表格视图上拥有任何自定义页脚都必须停止为其下方的空白行绘制行分隔符。那将是可怕的。作为参考,我查看了行数多于屏幕上无法查看的表,然后查看了包含两行的表。在这两种情况下,都没有多余的分隔符。
Perhaps your custom views were not actually added. To check that, set the background color to something other than clearColor
, e.g., [UIColor redColor]
. If you don't see some red bars at the bottom of the table, your footer wasn't set.
也许您的自定义视图实际上并未添加。要检查这一点,请将背景颜色设置为 以外的其他颜色clearColor
,例如,[UIColor redColor]
。如果您没有在表格底部看到一些红色条,则说明您的页脚未设置。
回答by dineshthamburu
Removing extra separator lines for empty rows in UITableView in Swift
在Swift 中删除 UITableView 中空行的额外分隔线
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.yourTableview.tableFooterView = UIView()
}
回答by Guntis Treulands
I would like to extend wkwanswer:
我想扩展wkw答案:
Simply adding only footer with height 0 will do the trick. (tested on sdk 4.2, 4.4.1)
只需添加高度为 0 的页脚即可。(在 sdk 4.2、4.4.1 上测试)
- (void) addFooter
{
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];
[self.myTableView setTableFooterView:v];
}
or even simpler - where you set up your tableview, add this line:
甚至更简单 - 在您设置 tableview 的地方,添加以下行:
//change height value if extra space is needed at the bottom.
[_tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)]];
or even simplier - to simply remove any separators:
甚至更简单 - 只需删除任何分隔符:
[_tableView setTableFooterView:[UIView new]];
Thanks to wkwagain :)
再次感谢wkw:)
回答by Kyle Clegg
For iOS 7+ using Storyboards
对于使用故事板的 iOS 7+
Simply drag a UIView
into your UITableView
as the footer. Set the footer view's height to 0.
只需将 aUIView
拖入您UITableView
的页脚即可。将页脚视图的高度设置为 0。
回答by Ambili B Menon
Try this. It worked for me:
尝试这个。它对我有用:
- (void) viewDidLoad
{
[super viewDidLoad];
// Without ARC
//self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
// With ARC, tried on Xcode 5
self.tableView.tableFooterView = [UIView new];
}
回答by Sebastian
For Swift:
对于斯威夫特:
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableFooterView = UIView()
}
回答by Babatunde Adeyemi
If you are using Swift, add the following code to viewDidLoadof the controller that manages the tableview:
如果您使用的是 Swift,请将以下代码添加到管理 tableview 的控制器的viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
//...
// Remove extra separators
tableView.tableFooterView = UIView()
}
回答by Darko
You can just add an empty footer at the end then it will hide the empty cells but it will also look quite ugly:
您可以在最后添加一个空页脚,然后它将隐藏空单元格,但它看起来也很丑陋:
tableView.tableFooterView = UIView()
There is a better approach: add a 1 point line at the end of the table view as the footer and the empty cells will also not been shown anymore.
有一个更好的方法:在表格视图的末尾添加一条 1 点线作为页脚,空单元格也将不再显示。
let footerView = UIView()
footerView.frame = CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1)
footerView.backgroundColor = tableView.separatorColor
tableView.tableFooterView = footerView
回答by roy
just add this code (Swift) . .
只需添加此代码 ( Swift)。.
tableView.tableFooterView = UIView()