大佬教程收集整理的这篇文章主要介绍了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的帮助
以上是大佬教程为你收集整理的Swift UICollectionView Cell 被渲染两次全部内容,希望文章能够帮你解决Swift UICollectionView Cell 被渲染两次所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。