2021年1月20日星期三

Swift UICollectionView Cell being rendered twice

I am writing a customer calendar as a challenge for me to practice UICollectionView. This issue has been puzzled me for few days. So the problem is when it reuses the cell, the load data source function has been called twice or three times, then it leads to:

This is how it looks like when function is called twice

Sometimes it's called three times and my calendar days become 100 110 etc.

Here is the delegate and datasource code:

extension CalendarViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {            func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {          return self.calendarPages.count      }            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CalendarCell          cell.calendarPage = self.calendarPages[self.currentPageIndex]                    cell.contentView.addSubview(self.buildCalendarPage(index: indexPath.row, frame: cell.contentView.frame)) // this is where it add new month page to the calendar, and it's randomly called multiple times               print("cell called")          return cell      }            func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {                    return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)      }  }  
https://stackoverflow.com/questions/65820215/swift-uicollectionview-cell-being-rendered-twice January 21, 2021 at 10:02AM

没有评论:

发表评论