ios – 滚动Swift时tableView值的重复

ios – 滚动Swift时tableView值的重复,第1张

概述我有一个tableView来显示工作日的时间 timingsArray在tableView中用于显示时序 timingsArray类型timingObj class timingObj { var dayNo: Int? var open: String? var close: String? var dayName: String? init(json 我有一个tableVIEw来显示工作日的时间

timingsArray在tableVIEw中用于显示时序

timingsArray类型timingObj

class timingObj {    var dayNo: Int?    var open: String?    var close: String?    var dayname: String?     init(Json: JsON){        // to take data from Json    }    init(name: String,number: Int){        self.dayname = name        self.dayNo = number    }}

在tableVIEw中我想显示所有日期及其时间,如果当天没有时间,则计时单元格将为空

let cell = tableVIEw.dequeueReusableCellWithIDentifIEr("TimingsCell",forIndexPath: indexPath) as! timingCell                 let timings = self.Details!.timingsArray            let dayTime = timings[indexPath.row]            cell.dayLabel.text = dayTime.dayname            if dayTime.open != nil && dayTime.close != nil {                cell.timeLabel.text = "\(convertTimeFormat(dayTime.open!)) - \(convertTimeFormat(dayTime.close!))"            }            return cell

这是细胞的类

class timingCell: UItableVIEwCell {    @IBOutlet weak var dayLabel: UILabel!    @IBOutlet weak var timeLabel: UILabel!}

例如星期二和星期四只有时间

问题是当我向上和向下滚动时,周二的定时值在周三重复,当我继续滚动时,定时值在其他单元格中也会重复

有任何想法吗?先感谢您

解决方法 来自Apple Docs:

At runtime,the table vIEw stores cell objects in an internal queue. When the table vIEw asks the data source to configure a cell object for display,the data source can access the queued object by sending a dequeueReusableCellWithIDentifIEr: message to the table vIEw,passing in a reuse IDentifIEr. The data source sets the content of the cell and any special propertIEs before returning it. This reuse of cell objects is a performance enhancement because it eliminates the overhead of cell creation.

因此,在重用队列中的单元格时,您正在执行的 *** 作是检索已创建和配置的单元格.对创建的单元格所做的更改也将应用于重用的单元格,这些单元格在重新使用的单元格不再可见时立即滚动显示.这意味着您必须重新分配要覆盖的任何值,这些值是您要显示的唯一单元格的自定义,如果没有要显示的内容/空字符串,这也适用.否则,您将获得随机的结果.

let cell = tableVIEw.dequeueReusableCellWithIDentifIEr("TimingsCell",forIndexPath: indexPath) as! timingCell     let timings = self.Details!.timingsArraylet dayTime = timings[indexPath.row]cell.dayLabel.text = dayTime.daynameif dayTime.open != nil && dayTime.close != nil {    cell.timeLabel.text = "\(convertTimeFormat(dayTime.open!)) - \(convertTimeFormat(dayTime.close!))"}else{ //change label to empty string when reusing cell.timeLabel.text = "";}return cell
总结

以上是内存溢出为你收集整理的ios – 滚动Swift时tableView值的重复全部内容,希望文章能够帮你解决ios – 滚动Swift时tableView值的重复所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1005078.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存