Objective c 如何删除分组UITable视图顶部的空白?

Objective c 如何删除分组UITable视图顶部的空白?,objective-c,uitableview,Objective C,Uitableview,当您使用UITableViewStyleGrouped样式创建UITableView时,它会在实际的tableviewcells和表格框架的边框之间添加相当多的空间。此空间位于tableviewcells的上方、下方、左侧和右侧 您可以通过以下方式更改TableViewCell之间的空间: [myTableView setSectionHeaderHeight:0]; [myTableView setSectionFooterHeight:25]; 然而,即使这样做,在框架顶部和第一个tabl

当您使用
UITableViewStyleGrouped
样式创建
UITableView
时,它会在实际的tableviewcells和表格框架的边框之间添加相当多的空间。此空间位于tableviewcells的上方、下方、左侧和右侧

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

[myTableView setSectionHeaderHeight:0];
[myTableView setSectionFooterHeight:25];
然而,即使这样做,在框架顶部和第一个tableviewcell之间仍然存在令人讨厌的空间。此外,在框架的左侧和tableviewcell之间,以及框架的右侧和tableviewcell之间还有大量的空间

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


有没有办法操纵该空间(调整其大小)?到目前为止,我唯一的解决方案是使帧的大小大于屏幕,从而使程序在屏幕外有“空白”,从而将其删除。然而,这显然不是最优的,而且是一个明确的问题。

这个答案来得很晚,但我希望它能帮助一些人

空间之所以存在,是因为
UITableView
tableHeaderView
属性。当
tableHeaderView
属性为
nil
时,苹果默认为一个视图。因此,解决方法是创建高度大于
0
的空视图。设置此选项将覆盖默认视图,从而删除不需要的空间

这可以在情节提要中通过将视图拖动到
表视图的顶部,然后将视图的高度设置为
1
或更大的值来完成

也可以使用以下代码以编程方式完成:

目标-C:

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)
Swift:

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)

评论 正如其他人所指出的,您可以对页脚使用相同的解决方案


来源和确认 有关
tableHeaderView
属性的更多详细信息,请参阅

感谢@liushuaikobe使用最小正态数进行验证。

单线解决方案:

目标-C

self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
迅捷的


如果tableView的样式为
UITableViewStyleGrouped
,则必须注意截面页眉或截面页脚高度的委托,因为在这种情况下需要实现这一点

返回值不应为0,即使SectionHeader或SectionFooter的高度为0,也需要非常小的值;尝试
CGFLOAT\u MIN

以我为例:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (section == [self.dataArray indexOfObject:self.bannerList]) {
    return 46;
}
return CGFLOAT_MIN;
}

确保您实现了这两种方法,并且该值正确,并且顶部边距将是固定的。

Swift 5以后:

tableView.contentInsetAdjustmentBehavior = .never
老斯威夫特。在
viewDidLoad()方法中使用它

tableView.tableHeaderView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 0.0, height: Double.leastNormalMagnitude))

你也需要设置页脚

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
  return [[UIView alloc] initWithFrame:CGRectZero];   
}

适用于iOS 11.0+

tableView.contentInsetAdjustmentBehavior = .never
迅速回答

如果在interface builder中选择了表格视图,并且在属性检查器中选择了样式“分组”,请在视图控制器中输入以下代码以解决额外的标题空间问题

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return CGFloat.leastNonzeroMagnitude
}
位置图标下方是顶部间距为零的表格


我必须将两种委托功能结合起来才能使其工作:

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))
}

上述解决方案对我都不起作用

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


为了实现这种定制行为,我建议正确的方法:
UICollectionView
在阅读了有关滚动视图插图和默认contentEdgeInsets等的10个其他答案后,这似乎是UIViewController中嵌入分组样式的最佳解决方案。单行解决方案
self.tableView.tableHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0,0,0,CGFLOAT\u MIN)]
@liushuaikobe:
CGFLOAT\u MIN
有效吗?我似乎记得在写答案时尝试了这个,但没有成功。如果你调用heightForHeaderInSection,不要返回零,否则上面的代码将不起作用。你可以简单地使用:
tableView.tableHeaderView=UIView(帧:CGRect(x:0.0,y:0.0,宽度:0.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))
}