我想用:
实现一个表看起来像这样:
我不知道如何实现各个部分之间的空间。
a)通常的方法是设置节标题或节页脚的高度。
b)我试图让页脚包含各个部分之间的空格,但粘性页脚也包含这个看起来很难看的空间:
这是我配置每个部分的页眉和页脚的方式:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 80
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let contentView = UIView()
......
return contentView
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let contentView = UIView()
......
return contentView
}
我需要自定义页眉和页脚,因为我想使用那些的粘性功能,意味着在表格顶部将显示当前标题部分,并在底部显示最后一个可见部分页脚(因此这些随滚动更改)。
答案 0 :(得分:3)
据我所知,没有" easy"做你想做的事。但它也不是那么难。正如@Tom建议您需要为空白空间使用空单元格。但是空单元格必须放在它没有页眉或页脚的部分中。
我把一些非常快速的东西放在一起让你开始(将它添加到Playground以查看它的实际效果)。在图像中,波纹管标题为绿色,内容单元格为蓝色,页脚为黄色,间隔符为白色:
import UIKit
import PlaygroundSupport
class ContentCell: UITableViewCell {
}
class SectionSpacerCell: UITableViewCell {
}
class MyViewController : UIViewController, UITableViewDataSource, UITableViewDelegate {
var tableView: UITableView!
private var sections: [Int] = []
override func loadView() {
let view = UIView()
view.backgroundColor = .white
tableView = UITableView(frame: view.bounds)
tableView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
tableView.register(ContentCell.self, forCellReuseIdentifier: "ContentCell")
tableView.register(SectionSpacerCell.self, forCellReuseIdentifier: "SectionSpacerCell")
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
self.view = view
}
override func viewDidLoad() {
super.viewDidLoad()
sections = [4, 7, 4, 2, 7]
tableView.reloadData()
}
// MARK: UITableViewDataSource
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count > 0 ? sections.count + (sections.count - 1) : 0
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section % 2 == 0 ? sections[section / 2] : 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section % 2 == 0 {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "ContentCell") as? ContentCell else {
fatalError("Oops!")
}
cell.backgroundColor = .blue
return cell
}
else {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SectionSpacerCell") as? SectionSpacerCell else {
fatalError("Oops!")
}
cell.backgroundColor = .white
return cell
}
}
// MARK: - UITableViewDelegate
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section % 2 == 0 {
return 40
}
return 0
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section % 2 == 0 {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
headerView.backgroundColor = .green
return headerView
}
return nil
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
if section % 2 == 0 {
return 40
}
return 0
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
if section % 2 == 0 {
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
headerView.backgroundColor = .yellow
return headerView
}
return nil
}
}
PlaygroundPage.current.liveView = MyViewController()
答案 1 :(得分:0)
使用heightForFooterInSection UITableViewDelegate
功能:
https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614967-tableview
您可以改变不同部分之间的大小;所以对于最后一节,返回0(或任何有意义的)
答案 2 :(得分:0)
如何添加"空白空间"除了最后一节之外的每个部分的表视图行?