60

在 iOS 6 中,分组表视图的底部似乎有额外的填充(iOS 5 没有),但我找不到任何表明这是正确/预期行为的文档。

这也会影响示例项目,例如示例中的SimpleTableView项目TableViewSuite。我想我必须将样式更改AppDelegate为“分组”,并将 SDK 更新到 iOS 6,但没有对项目进行其他更改。

调查显示,10px保留了页眉和页脚视图,还有一些20px无法解释。没有实际的页眉或页脚视图(tableHeaderViewtableFooterVieware nil,并且实施和返回nil例如viewForFooterInSection什么都不做)。我在 tableView 本身上找不到任何“20”值,尽管我当然可能错过了一些东西。

为页脚添加一个零大小的视图没有任何作用,但添加一个1px方形视图会导致额外的填充消失。例如:

tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0,0,1,1)];

它仍然占用1px高度,所以底部填充现在是11px,但这远不如 20 明显。现在将 设置sectionFooterHeight为 0 将只1px导致底部空间。

我的问题是:什么?我怎样才能完全删除它?这不是任何关键任务,但它非常奇怪,不可取,而且据我所知,它是无证的。

请注意 - 它从苹果开发论坛复制过去的问题。但我有完全相同的问题,我也不明白如何解决它。

4

12 回答 12

88

@frankWhite 的解决方案效果很好,但这里有一个更好的解决方案,可以避免输入 0.1、0.001 或其他内容以减少歧义。

// Swift version
func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {

    // remove bottom extra 20px space.
    return CGFloat.min
}
// Objective-C version
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    // remove bottom extra 20px space.
    return CGFLOAT_MIN;
} 
于 2015-07-16T05:11:53.480 回答
33

您可以使用contentInset来补偿 20px 额外的底部边距:

tableView.contentInset = UIEdgeInsetsMake(0, 0, -20, 0);
于 2013-07-05T16:21:59.910 回答
17

在斯威夫特 3

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return CGFloat.leastNormalMagnitude
}
于 2016-09-27T21:15:38.337 回答
13
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   return 0.01;
} 

这对我有用

于 2015-04-08T15:39:24.140 回答
3

这是我猜想的唯一可行的解​​决方案。我尝试设置tableFooterView和. 但是什么也没发生。然后以下解决方案起作用了。返回零“返回0;” 没有效果。我们应该返回尽可能低的高度。tableHeaderViewnil

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

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

注意: 这是我打印值时得到的。

CGFLOAT_MAX = 340282346638528859811704183484516925440.000000

CGFLOAT_MIN = 0.000000

于 2016-02-09T06:31:25.863 回答
2

最近,我发现了这个问题,并按照上面发布的解决方案,但它们都不起作用。就我而言,我有一个具有动态高度的节标题。受上述解决方案的启发,我输入了以下代码,问题就解决了。我希望这会有所帮助。

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

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

并像这样配置 tableView 的页眉和页脚:

tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 0.1))
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 0.1))
于 2019-03-05T03:00:47.780 回答
1

在 Swift 4.2 中,您可以尝试设置具有最小正常幅度高度的表头 -

var frame = CGRect.zero
frame.size.height = .leastNormalMagnitude
tableView.tableHeaderView = UIView(frame: frame)
于 2018-12-18T06:02:43.577 回答
0

以下代码对我有用

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

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
于 2013-01-21T06:13:49.710 回答
0

斯威夫特 3 代码

class PaddingLessTableView : UITableView
{
    override func layoutSubviews() {
        self.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        super.layoutSubviews()
    }
}
于 2017-01-30T18:17:52.597 回答
0

这解决了这个问题:

对象-c

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

迅速:

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 0.1
}
于 2017-02-05T02:07:57.710 回答
0

斯威夫特 5

 tableView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: .leastNormalMagnitude))
于 2019-10-17T05:33:05.307 回答
-2

使用 的bounces属性UIScrollView

[yourTableView setBounces:NO];

这将删除您的UITableView. 实际上,它只会禁用 tableview 的滚动视图以滚动到内容的边缘。

于 2013-03-15T03:16:09.400 回答