当您使用UITableView
样式创建UITableViewStyleGrouped
时,它会在实际的tableviewcells和表格框架的边框之间添加相当多的空间(或“缓冲”)。这个空间位于tableviewcells的上方,下方,左侧和右侧。
您可以通过以下方式更改tableviewcells之间的空间:
[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];
然而,即使通过这样做,在帧的顶部和第一个tableviewcell之间仍然存在这个烦人的空间。此外,框架左侧和桌面单元之间仍有大量空间,框架右侧和桌面单元也是如此。
- = - = - = - = - = - = -
基本上,有没有办法操纵那个空间(调整它的大小)?到目前为止,我唯一的解决方案是使框架的尺寸大于屏幕,以使程序伪造成在屏幕外面具有“空白空间”,从而将其移除。然而,这显然不是最佳的,也不是一个明确的黑客攻击。
有什么想法?提前谢谢!
答案 0 :(得分:60)
这个答案来得很晚,但我希望它对某人有帮助。
由于UITableView
的{{1}}属性,空间在那里。当tableHeaderView
属性为tableHeaderView
时,Apple默认显示视图。所以解决这个问题的方法是创建一个高度大于nil
的空视图。设置此选项会覆盖默认视图,从而删除不需要的空间。
这可以在故事板中完成,方法是将视图拖到0
的顶部,然后将视图的高度设置为tableView
或更高的值。
或者可以使用以下代码以编程方式完成:
<强>目标-C:强>
1
<强>夫特:强>
CGRect frame = CGRectZero;
frame.size.height = CGFLOAT_MIN;
[self.tableView setTableHeaderView:[[UIView alloc] initWithFrame:frame]];
正如其他人所说,你可以使用同样的解决方案来管理页脚。
有关var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)
属性的详细信息,请参阅Documentation。
感谢@liushuaikobe使用最不正数的正常数字进行验证。
答案 1 :(得分:5)
单线解决方案:
目标-C
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
夫特
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double(FLT_MIN)))
答案 2 :(得分:2)
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
下面的位置图标是顶部间距为零的表格。
答案 3 :(得分:1)
如果tableView的样式为UITableViewStyleGrouped
,那么你必须注意SectionHeader或SectionFooter高度的委托,因为这需要在这种情况下正确实现。
返回值不应为0,即使SectionHeader或SectionFooter的高度为0,它也需要是一个非常小的值;试试CGFLOAT_MIN
。
我的例子:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
return 46;
}
return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return CGFLOAT_MIN;
}
确保您实现了这两种方法,并且值正确,并且上边距将被修复。
答案 4 :(得分:1)
Leszek非常好的回答。但由于FLT_MIN
为deprecated
,因此以下代码更好。并在viewDidLoad()
方法中使用它。
夫特
self.tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double(Float.leastNormalMagnitude)))
答案 5 :(得分:0)
你也需要设置页脚
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return [[UIView alloc] initWithFrame:CGRectZero];
}
答案 6 :(得分:0)
对于iOS 11.0 +
tableView.contentInsetAdjustmentBehavior = .never
答案 7 :(得分:0)
Swift 4中的答案
如果在界面构建器中选择了表视图,并且在属性检查器中选择了“分组”样式,请在视图控制器中输入以下代码以解决额外的标题空间问题。
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return CGFloat.leastNonzeroMagnitude
}
答案 8 :(得分:0)
我必须结合两个委托函数才能工作:
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: .leastNonzeroMagnitude))
}
答案 9 :(得分:0)
答案 10 :(得分:-4)
对于C#:
11