【发布时间】:2021-11-26 09:51:11
【问题描述】:
UITableView 具有多个没有节页脚的节,显示节之间有额外的空格。 Xcode 的视图调试器显示它不是视图,而只是一个空白区域。
在我的情况下,这种行为是不受欢迎的。
添加 1.0/0.0 高度的页脚并没有帮助。更改表格视图的style 也不会。
这是一个示例代码:
import UIKit
final class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = .yellow
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = UIView()
header.backgroundColor = .green
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 20.0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.backgroundColor = .blue
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 30.0
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let footer = UIView()
footer.backgroundColor = .red
return footer
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 20.0
}
}
这是 iOS 14 和 iOS 15 中的输出:
【问题讨论】:
-
重复问题...请参阅此处以解决您的问题:stackoverflow.com/a/69350529/6883935
-
@EduardoSanti 不,不是。在您指出的问题中,有人询问其表格视图周围的边距。我询问部分之间的空白。接受的答案包含我的问题的解决方案,但该问题的作者说这不是他们问题的解决方案。
-
对不起,我看错了。你在使用 heightForHeaderInSection 委托方法吗?
-
我尝试使用
heightForHeaderInSection和不使用,并尝试使用 0.0 和 1.0 高度 - 它不影响空间。正如我所提到的,Xcode 的视图调试器显示部分之间没有标题(或任何其他视图),只有一个空白区域。而这个空间只出现在 iOS 15 中。 -
可以分享图片吗?