有的时候,我们创建的tableView会不确定它的frame,里面的cell不够多的话,就会产生这种情况。
我们要怎么解决呢。想了好久,想到以前看到的为什么要添加一个尾部视图,就是footView,于是尝试着添加进去,然后出现了自己想要的结果。
这真是太意外了,原来tableView的tableView有这个作用。
仔细想了一下。我们可以一开始就定义tableView的frame,让它的高度设置为我们让它可以达到的最大的高度。
然后添加一个footView,就可以达到即使cell不够多,我们的tableView也不会显示一大串的空白cell来填充tableView的其他frame的空白部分。
我把部分代码复制出来:
#pragma mark createTableView
-(void)createTableView{
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-20-44)];
_tableView.dataSource = self;
_tableView.delegate = self;
_tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
[self.view addSubview:_tableView];
}
#pragma mark tableView代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 50;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
SMEndView * endView = [[SMEndView alloc]init];
endView.frame = CGRectMake(0, 0, ScreenWidth, 50);
return endView;
}