簡體   English   中英

當 collectionview 刷新時,CollectionView 單元格的計時器會混淆

[英]CollectionView cell's timers get mixed up when collectionview refreshes

我有一個 collectionView,它在每個單元格中包含計時器。 當應用程序啟動時,所有計時器都可以正常工作,但是,當我刷新集合視圖時,所有計時器都會混淆並且到處都是。 我很確定這與之前的計時器沒有關閉有關。

集合視圖的代碼:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "contestsCell", for: indexPath) as! ContestsCollectionViewCell
    cell.nameLabel.text = listingArray[indexPath.row].name


    let arr = [listingArray[indexPath.row].expiryDate, cell.expiryDateLabel] as [AnyObject]

    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCounting), userInfo: arr, repeats: true)

    return cell
}

定時器功能代碼:

@objc func updateCounting(timer:Timer){
    // here we set the current date

    var dateArray: [AnyObject] = timer.userInfo as! [AnyObject]

    let itemDate = dateArray[0] as! String
    var dateList = itemDate.components(separatedBy: "-")

    let dateLabel = dateArray[1] as! UILabel

    let date = NSDate()
    let calendar = Calendar.current

    let components = calendar.dateComponents([.hour, .minute, .month, .year, .day, .second], from: date as Date)

    let currentDate = calendar.date(from: components)

    let userCalendar = Calendar.current

    // here we set the due date. When the timer is supposed to finish
    let competitionDate = NSDateComponents()
    competitionDate.year = Int(dateList[0])!
    competitionDate.month = Int(dateList[1])!
    competitionDate.day = Int(dateList[2])!
    competitionDate.hour = 00
    competitionDate.minute = 00
    competitionDate.second = 00
    let competitionDay = userCalendar.date(from: competitionDate as DateComponents)!

    //here we change the seconds to hours,minutes and days
    let CompetitionDayDifference = calendar.dateComponents([.day, .hour, .minute, .second], from: currentDate!, to: competitionDay)


    //finally, here we set the variable to our remaining time
    let daysLeft = CompetitionDayDifference.day
    let hoursLeft = CompetitionDayDifference.hour
    let minutesLeft = CompetitionDayDifference.minute
    let secondsLeft = CompetitionDayDifference.second

    if competitionDay > currentDate as! Date {
        //Set countdown label text
        dateLabel.text = "\(daysLeft ?? 0): \(hoursLeft ?? 0): \(minutesLeft ?? 0): \(secondsLeft ?? 0)"
    }

}

刷新collectionView的代碼

@objc func loadData() {

    retrieveContests()
    listingArray.removeAll()
    self.refresher.endRefreshing()
}

我認為您的問題與您的單元格出列有關。 因為這背后的原理是現有的單元格對象正在被 iOS 重用,這可能會導致一些問題。

例如,在您創建帶有 countdown1 和計時器的 cell1 的方法中,使用 cell2 和 countdown2 (+timer) 也是如此。 一旦你重新加載你的 collectionView cell1 和 cell2 將被重用,但現在 cell1 將有一個不同的索引路徑,因此用於 countdown2 並且還創建了一個新的計時器。

這可能會導致您的計時器在此過程中混淆的問題,據我在您的代碼中看到的,您也從未停止任何計時器,因此我會說您的假設是正確的。 每次重新加載集合視圖時,都會創建更多計時器。

我解決這個問題的建議是:在每個包含用於計算倒計時的日期對象的單元格上創建一個屬性。 然后只需使用一個計時器通知所有單元格自行更新其內容。 也許您可以為此使用 NotificationCenter。 這(或類似的方法)應該可以幫助您並避免維護所有這些計時器的整個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM