我在表视图中有两个Xib,一个是单元格自定义UITableViewCell Xib,第二个是UIView自定义视图xib(即footerView
)。在单击日期单元格时,它将显示我的约会列表。 (图片仅供参考)。我从SQLite
获取数据,然后将记录追加到模型。
footerView
中的约会列表。首先,它加载数据时很好。当我移动/查看tableView或返回同一页面时,它显示重复项。 for loop
在footerView中显示约会列表记录。 (在cellForRowAt中)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?
}
TableView:
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或通过在模型类中保留bool值来实现此目的。
单元格已重复使用。这意味着当您重新使用该单元格时,对该单元格所做的任何操作仍然有效。
根据您的情况,每次调用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)
}