簡體   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