我想实现带有部分的表格,但我希望只有一个部分应该有标题,并且对于所有部分,这个单独的标题应该显示为 float 。
有没有人有解决办法,请帮助我。
最佳答案
我同意 SuperDuperTango 在这种情况下只使用 one 部分。转换分段数据源的简单方法如下所示:
struct Section {
let title: String
let rows: [Row]
}
struct Row {
let title: String
}
class TableViewController: UITableViewController {
// original data source
let sections: [Section] = {
var sections = [Section]()
for section in ["A", "B", "C", "D", "E"] {
let numberOfRows = 1...Int.random(in: 1...5)
let rows = numberOfRows.map { Row(title: "Section \(section), Row \($0)") }
let section = Section(title: "Section \(section)", rows: rows)
sections.append(section)
}
return sections
}()
// transformed data source
var allRows: [Row] {
return sections.reduce([], { $0 + $1.rows })
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Your section title"
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return allRows.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = allRows[indexPath.row].title
return cell
}
}
关于ios - 具有一个 float 标题的 Tableview 多个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56553258/