ios - HeaderCell 仅被添加到表视图一次
问题描述
为了显示每个用户输入,我创建了一个显示结构数组的表格视图。它工作正常,但我目前正在尝试向headerCell
每个显示输入日期的条目添加一个。
因此,我创建了另一个名为DateCell
显示日期的单元格。另外我添加了:func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int)
到 TableViewController。
我的方法有效,但只是部分有效 -DateCell
正在显示,但只显示一次,所有timelineCells
条目都包含在下面。每次添加一个条目并因此timelineCell
添加 a 时,其中的日期DateCell
都只会被更新,但我希望每个条目timelineCell
都有DateCell
自己的日期。
表视图控制器
class TimelineViewController: UIViewController {
@IBOutlet weak var toolbar: UIToolbar!
@IBOutlet weak var timlineView: UITableView!
@IBOutlet weak var buttonBack: UIBarButtonItem!
let defaults = UserDefaults.standard
var isAsc = false
override func viewDidLoad() {
super.viewDidLoad()
sortArray()
self.setToolbarInvisible(toolbar: toolbar)
timlineView.delegate = self
timlineView.dataSource = self
setShadow()
}
...
func sortArray() {
addDataArray.sort(by: { $1.date < $0.date })
}
}
extension TimelineViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return addDataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let rowData = addDataArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineCell") as! TimelineCell
cell.setDrivenKm(drivenKm: rowData.driven)
cell.setConsumedL(consumedL: rowData.consumedL)
cell.setPricePerLiter(pricePerLiter: rowData.pricePerLiter)
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let rowData = addDataArray[section]
let headerCell = tableView.dequeueReusableCell(withIdentifier: "DateCell") as! DateCell
headerCell.setDate(date: rowData.date)
return headerCell
}
}
标题单元格
class DateCell: UITableViewCell {
@IBOutlet weak var dateLabel: UILabel!
func setDate(date: Date) {
let date = date
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy"
let currentDate = formatter.string(from: date)
dateLabel.text = currentDate
}
}
解决方案
正如 Sean 在他的评论中提到的,您可以为 addDataArray 的每个条目创建一个部分。
您需要在每个部分中返回节数并将 numberOfRows 更改为 1。您还需要更改为时间轴单元格检索数据的方式,使用节而不是行。
所以你需要像这样改变你的 UITableViewDelegate 方法:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
func numberOfSections(in tableView: UITableView) -> Int {
return addDataArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let rowData = addDataArray[indexPath.section]
let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineCell") as! TimelineCell
cell.setDrivenKm(drivenKm: rowData.driven)
cell.setConsumedL(consumedL: rowData.consumedL)
cell.setPricePerLiter(pricePerLiter: rowData.pricePerLiter)
return cell
}
或者,您可以将单元格计数加倍并将标题和项目作为单元格返回
然后,您需要进行以下更改:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return addDataArray.count * 2
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row % 2 == 0 {
let rowData = addDataArray[indexPath.row / 2 ]
let headerCell = tableView.dequeueReusableCell(withIdentifier: "DateCell") as! DateCell
headerCell.setDate(date: rowData.date)
return headerCell
} else {
let rowData = addDataArray[indexPath.row / 2]
let cell = tableView.dequeueReusableCell(withIdentifier: "TimelineCell") as! TimelineCell
cell.setDrivenKm(drivenKm: rowData.driven)
cell.setConsumedL(consumedL: rowData.consumedL)
cell.setPricePerLiter(pricePerLiter: rowData.pricePerLiter)
return cell
}
}
我创建了一个游乐场来展示一个例子。在这里查看。
推荐阅读
- javascript - 用纯 Javascript 遍历多维数组
- python - Django Rest 框架返回字典而不是 OrderedDicts
- django - Pyinstaller 在 Django 静态 javascript 文件上编译但出现 404 错误
- go - golang 在 1 秒内每 1 毫秒执行一次函数(每秒 1000 次调用)
- rest - SprintBoot 内容类型为空
- angular - 如何以角度在组件之间传递选择器或html标签
- vba - 比较 2 个单元格,然后比较下面的 2 个单元格
- mongodb - 如果根 C 上已经存在数据文件夹怎么办
- google-vr - Google vr 视图使用点击热点更改内容
- php - 测试 PHP POST 空值 - 意外结果 - 我错过了什么?