如何解决Swift UICollectionView Cell 被渲染两次
我正在编写客户日历,作为练习 UICollectionView
的挑战。这个问题困扰了我好几天。所以问题是当它重用单元格时,加载数据源函数被调用了两三次,然后导致:
有时它会被调用 3 次,我的日历天变成 100 110 等。
这是委托和数据源代码:
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)
}
}
解决方法
我知道那里发生了什么,所以基本上我把它弄得太复杂了,在另一个 UICollectionView 中有一个日历 UICollectionView,(我只是想让它可以滚动),所以每次重复使用单元格时,我忘记重新加载界面
解决办法,只保留一个UICollectionView,每个cell为一天,另外创建一个UICollectionViewCell类来控制可复用的cell,感谢babar的帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。