我试图显示一个简单的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];
}
当前回答
我现在不能添加评论,所以添加这个作为答案。
@Andy的回答很好,下面这行代码也能达到同样的效果:
tableView.tableFooterView = [UIView new];
'new'方法属于NSObject类,并为UIView调用alloc和init方法。
其他回答
使用UITableViewController
接受的解决方案将改变TableViewCell的高度。要解决这个问题,请执行以下步骤:
在ViewDidLoad方法中编写如下代码片段。 表视图。tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; 在TableViewClass中添加以下方法。m文件。 - (CGFloat)tableView:(UITableView *)tableView hightforrowatindexpath:(NSIndexPath *)indexPath { 返回(单元格高度设置在storyboard); }
就是这样。您可以构建并运行您的项目。
在故事板中,选择UITableView,并将属性Style从Plain修改为Grouped。
或者你可以调用tableView方法来设置页脚高度为1点,它会添加最后一行,但你也可以隐藏它,通过设置页脚背景颜色。
代码:
func tableView(tableView: UITableView,heightForFooterInSection section: Int) -> CGFloat {
return 1
}
好像是最后一行
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)
我现在不能添加评论,所以添加这个作为答案。
@Andy的回答很好,下面这行代码也能达到同样的效果:
tableView.tableFooterView = [UIView new];
'new'方法属于NSObject类,并为UIView调用alloc和init方法。