首页 > 解决方案 > 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
    }

}

标签: iosswift

解决方案


正如 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
    }
}

我创建了一个游乐场来展示一个例子。在这里查看。


推荐阅读