从iOS7开始,在我的UITableView顶部有额外的空间它有一个UITableViewStyleGrouped样式。
这里有一个例子:
tableview从第一个箭头开始,有35个像素的无法解释的填充,然后绿色的头是一个由viewForHeaderInSection返回的UIView(其中section为0)。
有人能解释一下这个35像素的数量是从哪里来的吗?我如何才能在不切换到UITableViewStylePlain的情况下摆脱它?
更新(回答):
在iOS 11及更高版本中:
tableView.contentInsetAdjustmentBehavior = .never
你可以检测你的应用是否运行iOS7或更高版本,并在你的表视图委托中添加这两个方法(通常在你的UIViewController代码中)
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
这可能不是一个优雅的解决方案,但对我来说是可行的
斯威夫特版本:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
我注意到这个问题有很多答案,这取决于你想要做什么,所以我将分享我的答案,以防有人想要同样的效果:
在我的视图控制器中,我有一个分组的UITableView,它的tableHeaderView由一个200点高的UIScrollView和一个单独的带有属性的UILabel组成,UILabel包含一个广告标题、价格、位置和发布的时间。下面是表视图的内容。
在分组表视图标头的默认维度下,“MORE INFO”标头远远低于“5天前”标签。我通过覆盖每个部分的页眉和页脚的高度来修复它(已经是上面的图像)。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return section == kMoreInfoSection ? 33 : UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return UITableViewAutomaticDimension;
}
我从@erurainon的评论中了解了覆盖高度,从https://stackoverflow.com/a/14404104/855680了解了UITableViewAutomaticDimension。我不需要设置self。automyadjustsscrollviewinsets在我的视图控制器中不像接受的答案所建议的那样为NO。
我也已经尝试设置UIEdgeInsets和给表视图一个负值的顶部,但它没有工作-整个表视图移动,包括tableHeaderView。
你可以检测你的应用是否运行iOS7或更高版本,并在你的表视图委托中添加这两个方法(通常在你的UIViewController代码中)
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
这可能不是一个优雅的解决方案,但对我来说是可行的
斯威夫特版本:
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return CGFloat.leastNormalMagnitude
}
唯一对我有用的是:
迅速:
tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0
objective - c:
self.tableView.sectionHeaderHeight = 0;
self.tableView.sectionFooterHeight = 0;
另外,我还有多余的空间来写第一部分。这是因为我错误地使用了tableHeaderView属性。修正了,以及添加:
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 0.01))