在 tableView 的单元格页脚视图中复制行 swift

Duplicating rows within cell footer view of tableView swift

我在表视图中有两个 Xib,一个是单元格自定义 UITableViewCell Xib,第二个是 UIView 自定义视图 xib(footerView)。单击日期单元格时,它会显示约会列表。 (附上图片供参考)。 我从 SQLite 获取数据,然后将记录附加到模型。

How to avoid duplicating footer view rows in cell TableView?

减速: var downloadAppData: [DownloadDisplay] = []

型号:

struct DownloadDisplay : Codable {
    var date: Int64?
    var appointments: [Appointment]?
    var progress: Double?
    var isFinished: Bool?
}

struct Appointment : Codable {

    let appointmentHour : String?
    let backgroundColorDark : String?
    let backgroundColorLight : String?
    let controlHour : String?
    let date : String?
    let id : Int?
    let isProjectManual : Bool?
    let projectDistrict : String?
    let projectFirmName : String?
    let projectName : String?
    let projectType : String?
    let subTitle : String?
    let warmingType : String?
}

表格视图:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return downloadAppData.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DownloadEntryViewCell", for: indexPath) as! DownloadEntryViewCell
        let dic = downloadAppData[indexPath.row]
        let content = datasource[indexPath.row]


        let appDate = Date(milliseconds: dic.date ?? 0)        
        let dateFormatter = DateFormatter()
        dateFormatter.locale = Locale(identifier: "tr_TR_POSIX")
        dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        dateFormatter.dateFormat = "dd MMMM yyyy EEEE"
        let convertDate = dateFormatter.string(from: appDate)

        cell.fileNameLabel.text = "\(convertDate)" + " Randevuları"

        var stackHeight:CGFloat = 0.0

        // footer view xib. 
        for i in (dic.appointments)! {
            let child_view = Bundle.main.loadNibNamed("FooterView", owner: self, options: nil)?.first as! FooterView
            child_view.projName.text = "\(i.projectName ?? "")" + "  " + "\(i.subTitle ?? "")"
            cell.stackViewFooter.addArrangedSubview(child_view)
            stackHeight = stackHeight + 33.0
        }


        if content.expanded == false {

            cell.rightView.backgroundColor = ("#556f7b").toColor()
            cell.fileNameLabel.textColor = ("#eceff1").toColor()
            cell.progressLabel.textColor = ("#eceff1").toColor()
            cell.individualProgress.frame = CGRect(x: 0, y: 69, width: 360, height: 2)
            cell.individualProgress.progress = 0
            cell.individualProgress.backgroundColor = ("#cfd8dc").toColor()

       }

        return cell
    }

在添加子视图之前,只需检查它是否已经添加,因为每次滚动 tableView 时都会调用 cellForRow。您可以通过为子视图提供一个 viewTag 或在模型 class.

中维护一个 bool 值来做到这一点

细胞重复使用。这意味着您对该单元格所做的任何操作在该单元格被重复使用时仍然有效。

在您的情况下,每次调用 cellForRowAt 时,您都会向 stackView 添加(更多)子视图。

您可以:

A) 编辑您的单元格 class 并实施 prepareForReuse(),您将从 stackView

中删除任何现有子视图
override func prepareForReuse() {
    self.stackViewFooter.subviews.forEach {
        [=10=].removeFromSuperview()
    }
}

B)cellForRowAt

中添加新子视图之前删除子视图
    // first
    cell.stackViewFooter.subviews.forEach {
        [=11=].removeFromSuperview()
    }

    // then
    for i in (dic.appointments)! {
        ...
        cell.stackViewFooter.addArrangedSubview(child_view)
    }