滚动Swift时tableView的值重复 [英] Duplication in values of tableView when Scrolling Swift

查看:298
本文介绍了滚动Swift时tableView的值重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个tableView来显示工作日的时间

I have a tableView to display timings of week days

timingsArray在tableView中用于显示计时

timingsArray is used in tableView to display timings

timingObj类型的TimingsArray

timingsArray of type 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中,我想显示所有日期及其时间,如果该天没有时间,则时间单元将为空

in tableView i want to display all days and their timings, if the day does not have timing then timing cell will be empty

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!

}

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

e.g Tuesday and Thursday only have timings

问题是当我上下滚动时,星期二的计时值在星期三重复,而当我继续滚动时,计时值在其他单元格中也会重复

the problem is when i scroll up and down, timing value of Tuesday gets duplicated in Wednesday, and when i keep scrolling, timing values get duplicated too in other cells

有什么想法吗?预先谢谢你

Any ideas? Thank you in advance

推荐答案

来自Apple文档:

在运行时,表视图将单元格对象存储在内部队列中.当表视图要求数据源配置要显示的单元格对象时,数据源可以通过向表视图发送dequeueReusableCellWithIdentifier:消息并传递重用标识符来访问排队的对象.数据源在返回之前设置单元格的内容和任何特殊属性.单元对象的这种重用是一种性能增强,因为它消除了单元创建的开销.

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.

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

So what you're doing when reusing cells from the queue is retrieving cells you have already created and configured. Changes made to the created Cells will be also applied to the reused cells which are displayed when scrolling as soon as the reused cells aren't visible anymore. This means you have to re-assign any values you want to override which are custom to the unique cell you are displaying, which also applies if there is no content/an empty string you want to display. Otherwise you will get random results as you have been experiencing.

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!))"
}
else{
 //change label to empty string when reusing
 cell.timeLabel.text = "";
}

return cell

这篇关于滚动Swift时tableView的值重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆