繁体   English   中英

CollectionView单元格意外行为迅速

[英]CollectionView cell unexpected behaviour swift

我的单元格是根据timeSlotArray创建的, timeSlotArray以30分钟为增量保存所有打开时间段的时隙,但是根据其ID是否等于任何bookedTimeSlotsArray条目ID来获得其颜色。 如果我选择预订的日期,它将使单元格具有正确的颜色,但是即使我更改其他预订的日期,单元格也会在重新加载数据时保持该颜色。

该函数是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeSlotCell", for: indexPath) as! TimeSlotCollectionViewCell

    // Configure the cell
    cell.timeLabel.text = timeSlotArray[indexPath.row]

    cell.cellId = Int("\(String(describing: self.selectedDate))" + self.timeStringToStringConvert(timeSlotArray[indexPath.row]))

    if bookedTimeSlotsArray.count > 0 {
        for index in 0...bookedTimeSlotsArray.count - 1 {
            let bookingId = bookedTimeSlotsArray[index].bookingId

            if cell.cellId == bookingId {
                print("   match found")
                print("Index is: \(index)")
                print("cell time is: \(timeSlotArray[indexPath.row])")
                print("time slot cell id is: \(String(describing: cell.cellId))")
                print("booking id: \(bookingId)")
                cell.backgroundColor = UIColor.red.withAlphaComponent(0.3)
            } else {
                cell.backgroundColor = UIColor.green.withAlphaComponent(0.8)

            }
        }
    }
    return cell
}

我在两个函数calculateOpenTimeSlots()calculateBookedTimeSlots()重新加载视图数据,它们的调用方式如下:第一种情况:在viewDidLoad()我调用了两个函数, Firebase情况:在Firebase观察器函数中,我仅调用calculateBookedTimeSlots() ,第三个案例:从选择表视图单元格更改日期时,我只在didSelectRowAt调用了calculateOpenTimeSlots() 第一种和第三种情况按预期工作,但第二种情况与问题开头所描述的不同。 你们能看到我撞墙的地方吗? 和往常一样非常感谢。

编辑:

我在单元格的类中添加了prepareForReuse ,但是当集合视图绘制单元格时,我仍然得到相同的行为。 这是单元格类:

import UIKit

class TimeSlotCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var timeLabel: UILabel!

    var cellId: Int!

    override func prepareForReuse() {
        super.prepareForReuse()
        // Set your default background color, title color etc
        backgroundColor = UIColor.green.withAlphaComponent(0.8)
    }

}

找到了问题。 我取消了else后声明if在声明中cellForItemAt像现在prepareForReuse是设置默认的单元格。 现在一切都按预期工作。 最后的功能是:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeSlotCell", for: indexPath) as! TimeSlotCollectionViewCell

        // Configure the cell
        cell.timeLabel.text = timeSlotArray[indexPath.row]

        cell.cellId = Int("\(String(describing: self.selectedDate))" + self.timeStringToStringConvert(timeSlotArray[indexPath.row]))

        if bookedTimeSlotsArray.count > 0 {
            for index in 0...bookedTimeSlotsArray.count - 1 {
                let bookingId = bookedTimeSlotsArray[index].bookingId

                if cell.cellId == bookingId {
                    print("   match found")
                    print("Index is: \(index)")
                    print("cell time is: \(timeSlotArray[indexPath.row])")
                    print("time slot cell id is: \(String(describing: cell.cellId))")
                    print("booking id: \(bookingId)")
                    cell.backgroundColor = UIColor.red.withAlphaComponent(0.3)
                }
            }
        }
        return cell
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM