我试图显示一个简单的UITableView与一些数据。我希望设置UITableView的静态高度,这样它就不会在表的末尾显示空单元格。我怎么做呢?
代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%d", [arr count]);
return [arr count];
}
我试图显示一个简单的UITableView与一些数据。我希望设置UITableView的静态高度,这样它就不会在表的末尾显示空单元格。我怎么做呢?
代码:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"%d", [arr count]);
return [arr count];
}
当前回答
我试了一下代码:
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
在viewDidLoad部分,xcode6显示了一个警告。我在它前面放了一个“self.”,现在它可以正常工作了。所以我使用的工作代码是:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
其他回答
以下方法:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (([array count]*65) > [UIScreen mainScreen].bounds.size.height - 66)
{
Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [array count]*65));
}
else
{
Table.frame = CGRectMake(0, 66, self.view.frame.size.width, [UIScreen mainScreen].bounds.size.height - 66);
}
return [array count];
}
这里65是单元格的高度66是UIViewController中导航栏的高度。
Swift 3语法:
tableView.tableFooterView = UIView(frame: .zero)
Swift语法:< 2.0
tableView.tableFooterView = UIView(frame: CGRect.zeroRect)
Swift 2.0语法:
tableView.tableFooterView = UIView(frame: CGRect.zero)
我试了一下代码:
tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
在viewDidLoad部分,xcode6显示了一个警告。我在它前面放了一个“self.”,现在它可以正常工作了。所以我使用的工作代码是:
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
在Xcode 6.1上用swift实现
self.tableView.tableFooterView = UIView(frame: CGRectZero)
self.tableView.tableFooterView?.hidden = true
第二行代码不会对表示造成任何影响,您可以使用它来检查是否隐藏。
答案来自这个链接 未能在UITableView Swift中隐藏空单元格
在故事板中,选择UITableView,并将属性Style从Plain修改为Grouped。