[英]Duplicating rows within cell footer view of tableView swift
我在tableview中有两个Xib,一个是单元格自定义UITableViewCell Xib,第二个是UIView自定义视图xib(即footerView
)。 单击日期单元格时,它会显示我的约会列表。 (附上图片供参考)。 我从SQLite
获取数据,然后将记录附加到模型。
footerView
的约会列表。 首先当它加载数据时很好。 当我移动/滚动 tableView 或返回同一页面时,它显示重复。for loop
显示约会列表记录。 (在cellForRowAt 中)var downloadAppData: [DownloadDisplay] = []
没有重复的数据追加var downloadAppData: [DownloadDisplay] = []
如何避免在单元格 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
}
细胞被重复使用。 这意味着您对单元格所做的任何事情在该单元格被重用时仍然有效。
在您的情况下,每次调用cellForRowAt
,您都会向 stackView 添加(更多)子视图。
你可以:
A)编辑您的单元格类并实现prepareForReuse()
,您可以在其中从 stackView 中删除任何现有的子视图
override func prepareForReuse() {
self.stackViewFooter.subviews.forEach {
$0.removeFromSuperview()
}
}
或者
B)在cellForRowAt
添加新子视图之前删除子视图
// first
cell.stackViewFooter.subviews.forEach {
$0.removeFromSuperview()
}
// then
for i in (dic.appointments)! {
...
cell.stackViewFooter.addArrangedSubview(child_view)
}
在添加子视图之前,只需检查它是否已经添加,因为每次滚动 tableView 时都会调用 cellForRow。 您可以通过为子视图提供 viewTag 或在模型类中维护 bool 值来实现此目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.