减少 UITableView 部分之间的空间

有没有办法减少 UITableView 的两个部分之间的空间?我每个部分之间大约有15个像素。我已经尝试为 -tableView:heightForFooterInSection:-tableView:heightForHeaderInSection:返回0,但这不会改变任何东西。

有什么建议吗?

97361 次浏览

这有点棘手,但是试试这个代码:

- (CGFloat)tableView:(UITableView*)tableView
heightForHeaderInSection:(NSInteger)section {
if (section == 0) {
return 6.0;
}


return 1.0;
}


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


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


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

相应地更改值。删除空间,我认为0.0将不会被接受。最小的正常值似乎是1.0。

实际上,你可以在 size 选项卡下设置页脚/页眉/单元格高度的 Interface Builder。默认情况下,页眉/页脚设置为10.0。

你必须减少部分的页眉/页脚高度。然后部分之间的空间将会减少。

试试这个代码

这对我很有效: -)

tableView.sectionHeaderHeight = 2.0;
tableView.sectionFooterHeight = 2.0;

对于所有想把距离缩小到0的人,你必须使用:

tableView.sectionHeaderHeight = 0.0;
tableView.sectionFooterHeight = 0.0;

因为 UITableView氏委托的服务仅从大于零的浮点数开始产生效果。

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
return 1.0;
}




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


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


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

(使用 iOS 4.1和 XCode 4.0.2)

针对 iOS7的更新: 由于 ARC 自动发布更新,这里编辑了@Martin Stolz 代码。

-(CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
if(section == 0)
return 6;
return 1.0;
}


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


-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}


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

(使用 iOS7,Xcode v5.0)

你也可以从故事板中减少部分页脚和页眉的高度。在桌面视图-> 大小检查员。去部门高度。

Size inspector for UITableView in storyboard.

默认情况下,对于“普通样式”表,它被设置为22,对于“分组样式”表,它被设置为10。可以通过分别增加/减少页眉和页脚的值来配置值。

对我来说,问题在于我使用的是分组表视图样式(UITableViewStyleGrouped)。当我将它改为纯样式(UITableViewStylePlain)时,我的 tableView:heightForHeaderInSection:方法是决定每个节标题大小的唯一因素。

对我来说,这个问题只是通过使用以下代码就得到了解决,

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return section == 0 ? 1 : 20 // 20 is my other sections height
}

返回第一部分的 1解决了这个问题。

也许使用这个,0不会像预期的那样工作

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}


func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNormalMagnitude
}

要更改页眉/页脚空间,应实现以下方法 必须的:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?

还有

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat

(使用相应的方法更改页脚高度)

下面的代码完全删除了节周围的空格:

public func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return nil
}


public func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
return nil
}


public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}


public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return .leastNonzeroMagnitude
}

Swift 5,iOS 13 +

选项1-创建表视图时

tableView.sectionHeaderHeight = 0
tableView.sectionFooterHeight = 0

备选案文2-其他视图和部分更加灵活

// Top space for sections
public func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 0
}


// Bottom space for sections
public func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 0
}