从iOS7开始,在我的UITableView顶部有额外的空间它有一个UITableViewStyleGrouped样式。
这里有一个例子:
tableview从第一个箭头开始,有35个像素的无法解释的填充,然后绿色的头是一个由viewForHeaderInSection返回的UIView(其中section为0)。
有人能解释一下这个35像素的数量是从哪里来的吗?我如何才能在不切换到UITableViewStylePlain的情况下摆脱它?
更新(回答):
在iOS 11及更高版本中:
tableView.contentInsetAdjustmentBehavior = .never
我假设这只是新UITableViewStyleGrouped样式的一部分。它存在于所有分组表视图中,似乎没有任何直接的方法来控制该空间。
如果这个空间是由一个UIView表示的,那么可以搜索UITableView的所有子视图来找到那个特定的视图并直接编辑它。然而,也有可能该空间只是标题和单元格开始之前的硬编码偏移量,没有任何方法来编辑它。
要搜索所有子视图(我将在表没有单元格时运行这段代码,以使它更容易阅读输出):
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(@"%@", subview);
// List the subviews of subview
[self listSubviewsOfView:subview];
}
}
上面的很多答案都太俗气了。如果苹果决定修复这种意外的行为,它们在未来的任何时候都会崩溃。
问题的根源:
UITableView不喜欢头的高度为0.0。如果你要做的是有一个高度为0的标题,你可以跳到解决方案。
即使以后你给你的头分配了一个非0.0的高度,UITableView也不喜欢一开始就被分配一个高度为0.0的头。
解决方案:
然后,最简单可靠的修复方法是确保头高度在分配给表视图时不为0。
这样做是可行的:
// Replace UIView with whatever class you're using as your header below:
UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.tableView.bounds.size.width, CGFLOAT_MIN)];
self.tableView.tableHeaderView = tableViewHeaderView;
这样的事情会在某些时候(通常是在滚动之后)导致问题:
// Replace UIView with whatever class you're using as your header below:
UIView *tableViewHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
self.tableView.tableHeaderView = tableViewHeaderView;
下面的操作(Swift)解决了这个问题,但当你不需要头文件的时候,这是有效的。
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.min
}
如果你这样做,你将不得不放弃第一部分,并使用其他内容。
UITableViewDataSource实施:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return <number_of_data_sections>+1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// the first section we don't use for data
if section == 0 {
return 0
}
// starting from 1, there are sections we use
if section == 1 {
let dataSection = section - 1
// use dataSection for your content (useful, when data provided by fetched result controller). For example:
if let sectionInfo = myFRC!.sections![dataSection] as? NSFetchedResultsSectionInfo {
return sectionInfo.numberOfObjects
}
}
return 0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let dataIndexPath = NSIndexPath(forRow: indexPath.row, inSection: (indexPath.section - 1) )
// return cell using transformed dataIndexPath
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 1 {
// return your header height
}
return CGFloat.min
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 1 {
// return your header view
}
return nil
}
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
// in my case, even when 1st section header was of zero heigh, I saw the space, an that was a footer. I did not need footer at all, so always gave zero height
return CGFloat.min
}
就是这样。模型不知道任何变化,因为我们在访问数据时转换了节号。