ios - 使用一个浮动标题查看多个部分
问题描述
我想用部分实现表格,但我希望只有一个部分应该有标题,并且对于所有部分,这个单个标题应该显示为浮动。
有没有人有解决方案,请帮助我。
解决方案
我同意SuperDuperTango在这种情况下只使用一个部分。转换分段数据源的一种简单方法是:
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
}
}
推荐阅读
- python - 有没有办法在 OR-Tools 路由中设置特定的出发点和到达点?
- javascript - 404 Not Found for Web API 路由
- elasticsearch - 为什么弹性搜索返回与运算符“OR”无关的结果?
- tensorflow - 什么是深度学习中的融合内核(或融合层)?
- c# - c#中的多线程问题
- python - 对每一行数据框求和列,并在多级索引熊猫数据框中添加新列
- powershell - 连接后如何使用 PowerShell 发送 CLI 命令
- java - 如何使用 SpringBoot 从约 100 个客户数据库中查询和检索结果?
- asp.net - 不使用模拟时出现错误 401.3 未授权
- php - 从函数中清空全局变量