如何删除分组UITableView顶部的空白区域?

时间:2012-03-29 20:08:31

标签: objective-c uitableview

当您使用UITableView样式创建UITableViewStyleGrouped时,它会在实际的tableviewcells和表格框架的边框之间添加相当多的空间(或“缓冲”)。这个空间位于tableviewcells的上方,下方,左侧和右侧。

您可以通过以下方式更改tableviewcells之间的空间:

[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];

然而,即使通过这样做,在帧的顶部和第一个tableviewcell之间仍然存在这个烦人的空间。此外,框架左侧和桌面单元之间仍有大量空间,框架右侧和桌面单元也是如此。

- = - = - = - = - = - = -

基本上,有没有办法操纵那个空间(调整它的大小)?到目前为止,我唯一的解决方案是使框架的尺寸大于屏幕,以使程序伪造成在屏幕外面具有“空白空间”,从而将其移除。然而,这显然不是最佳的,也不是一个明确的黑客攻击。

有什么想法?提前谢谢!

11 个答案:

答案 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_MINdeprecated,因此以下代码更好。并在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)

以上解决方案都不适合我

在我的情况下,这些设置被设置为手动,我只是更改为自动

enter image description here

答案 10 :(得分:-4)

对于C#:

11