【发布时间】:2011-05-11 22:08:33
【问题描述】:
我的UITableView 有几个问题。
当我在我的页面上添加
UITableview时,默认情况下它会显示一些固定数量的行, 即使我将部分中的行数设置为1。除了第一行,所有行都出现了,和都是空行。所以,我想隐藏UItableview中的所有空行。基于非空行,我想改变我的
UItableView的高度。
【问题讨论】:
标签: ios uitableview
我的UITableView 有几个问题。
当我在我的页面上添加UITableview 时,默认情况下它会显示一些固定数量的行,
即使我将部分中的行数设置为1。除了第一行,所有行都出现了,和都是空行。所以,我想隐藏UItableview 中的所有空行。
基于非空行,我想改变我的UItableView的高度。
【问题讨论】:
标签: ios uitableview
在返回行数的方法中,自己动态指定count。例如,如果您要使用名为 arrayForRows: 的 NSArray 填充表格
return [arrayForRows count]; // inside the method which returns the number of rows.
上面是一个简单的例子,用一个数组作为数据源填充一个表。没有空行。根据填充表格的数组中的项目count,表格中只显示了那么多行。
我认为您的意思是表格中行的高度。您可以使用以下方法来调整行的高度:
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
编辑:啊,现在我明白了你想要实现的目标。您试图避免显示用这些分隔线分隔的空行,对吗?看这篇文章:
【讨论】:
您确定您使用的是UITableViewStyleGrouped 样式?
因为默认设置为UITableViewStylePlain,在显示填充单元格后也会显示空单元格。
【讨论】:
我通过创建一个简单的 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;
}
也许您还必须在表格视图中禁用滚动。
【讨论】:
新答案
在 Swift 2.2、3.0 及更高版本中,执行以下操作:
tableView.tableFooterView = UIView()
下面是旧答案。留给后代。
如果您必须使用UITableViewStylePlain,并且您没有将footerView 用于其他任何内容,那么如果您启用了ARC,则可以使用以下半脏解决方案。:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
return view;
}
如果您已禁用 ARC,请使用以下内容:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[[UIView alloc] init] autorelease];
return view;
}
这会创建一个不可见的 footerView,它会立即出现在最后一个填充数据的单元格之后。
【讨论】:
tableView.tableFooterView = UIView()
在 Bourne 的回答中有点隐藏;如果您想在具有多个部分的普通表格视图中将空行隐藏到底部,请使用以下答案:
【讨论】:
如果你的 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;
}
【讨论】:
- (void) viewDidLoad
{
[super viewDidLoad];
self.tableView.tableFooterView = [[[UIView alloc] init] autorelease];
}
【讨论】:
这也可以通过 Interface Builder 完成。只需添加一个 1 像素高的 UIView 作为 UITableView 的页脚。它与此处的大多数答案基本相同,但它在视图中而不是在视图控制器中保留了一些 UI 细节。
【讨论】:
[self.SomeTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
如果UITableView 为空,则使用此删除行。
【讨论】:
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.01f;
}
对于 iOS 7
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
【讨论】:
对于斯威夫特:
override func viewWillAppear(animated: Bool) {
self.tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
// OR
self.tableView.tableFooterView = UIView()
}
对于C#:(请不要忘记添加using CoreGraphics;)
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.sampleTableview.TableFooterView = new UIView(frame: CGRect.Empty);
}
【讨论】: