ios 如何隐藏 UITableView 中的空行并根据非空行更改 Uitableview 的高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4213197/
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
how to hide empty rows in a UITableView and change the height of the Uitableview based on non-empty rows
提问by sbmandav
I have couple of problems with my UITableView
.
我的UITableView
.
When I add a
UITableview
on my page, by default it brings up some fixed number of rows, even though I set number of rows in section as1
. All the rows appear except the first row, andall are empty rows. So, I want to hide all the empty rows in theUItableview
.Based on the non-empty rows, I want to change the height of my
UItableView
.
当我
UITableview
在页面上添加 a时,默认情况下它会显示一些固定数量的行,即使我将 section 中的行数设置为1
. 除第一行外的所有行都出现,并且都是空行。所以,我想隐藏UItableview
.基于非空行,我想更改我的
UItableView
.
采纳答案by Sanniv
You sure you are using UITableViewStyleGrouped
style?
你确定你用的是UITableViewStyleGrouped
风格?
Because by default it is set to UITableViewStylePlain
which shows empty cells too after displaying filled cells.
因为默认情况下,它被设置为UITableViewStylePlain
在显示填充单元格后也显示空单元格。
回答by ArtSabintsev
NEW ANSWER
新答案
In Swift 2.2, 3.0 and onwards, do the following:
在 Swift 2.2、3.0 及更高版本中,执行以下操作:
tableView.tableFooterView = UIView()
tableView.tableFooterView = UIView()
OLD ANSWER BELOW. KEPT FOR POSTERITY.
下面的旧答案。留作后代。
If you must use UITableViewStylePlain
, and you don't use a footerView for anything else, you can use the following semi-dirty solution if you have ARC enabled.:
如果您必须使用UITableViewStylePlain
,并且您不将 footerView 用于其他任何用途,则可以使用以下半脏解决方案(如果您启用了ARC ):
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
return view;
}
If you have ARC disabled, use the following:
如果您禁用了ARC ,请使用以下命令:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] init] autorelease];
return view;
}
This creates an invisible footerView, that appears immediately after the last data-filled cell.
这会创建一个不可见的 footerView,它会在最后一个填充数据的单元格之后立即出现。
回答by Sachin
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
and for iOS 7
并针对iOS 7
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
回答by Katlu
I solved the problem by creating a simple UIView as footer view with the same background color as the table background:
我通过创建一个简单的 UIView 作为页脚视图解决了这个问题,它的背景颜色与表格背景相同:
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)] autorelease];
view.backgroundColor = [UIColor whiteColor];
return view;
}
Maybe you have to disable scrolling in your table view in addition.
也许您还必须在表格视图中禁用滚动。
回答by codyko
If your tableview has multiple sections, you may try using this:
如果你的 tableview 有多个部分,你可以尝试使用这个:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return [UIView new];
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == tableView.numberOfSections - 1) {
return 1;
}
return 0;
}
回答by yunas
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
}
回答by Matt Becker
This can also be done through Interface Builder. Simply add a 1 pixel tall UIView as a footer to the UITableView. It's essentially the same as most of the answers here, but it keeps some UI specifics in the view instead of in the view controller.
这也可以通过 Interface Builder 来完成。只需添加一个 1 像素高的 UIView 作为 UITableView 的页脚。它本质上与此处的大多数答案相同,但它在视图中而不是在视图控制器中保留了一些 UI 细节。
回答by Code Guru
[self.SomeTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Use this to get rid of the lines if the UITableView
is empty.
如果UITableView
为空,则使用它来摆脱线条。
回答by Bourne
in the method that returns the number of rows, specify the count
yourself dynamically. For example if you are going to populate the table with say a NSArray
named arrayForRows:
在返回行数的方法中,count
动态指定自己。例如,如果您要使用NSArray
命名来填充表格arrayForRows:
return [arrayForRows count]; // inside the method which returns the number of rows.
The above is a simple example populating a table with an array as a datasource. There are no empty rows. Only that many rows show up in the table according to the count
of items in the array populating the table.
上面是一个简单的例子,用一个数组作为数据源填充一个表。没有空行。根据count
填充表格的数组中的项目,表格中只显示那么多行。
I think you mean height of the row in the table. You can play around with the height of the rows by using the method:
我认为您的意思是表格中行的高度。您可以使用以下方法来调整行的高度:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
EDIT: ah now I get what you are trying to achieve. You are trying to avoid showing the empty rows separated with those separator lines right? See this post:
编辑:啊现在我明白你想要达到的目标了。您试图避免显示用这些分隔线分隔的空行,对吗?看到这个帖子:
回答by A.G
For Swift:
对于斯威夫特:
override func viewWillAppear(animated: Bool) {
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
// OR
self.tableView.tableFooterView = UIView()
}
For C#: (Please don't forget to add using CoreGraphics;
)
对于 C#:(请不要忘记添加 using CoreGraphics;
)
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.sampleTableview.TableFooterView = new UIView(frame: CGRect.Empty);
}