ios - UICollectionView 不要求从 UICollectionViewDataSource 获取数据

标签 ios swift uicollectionview

我已经将自定义数据源设置为 UICollectionView:

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
let dataSource = DataSource(storage)
let delegate = DelegateFlow()
layout.itemSize = CGSize(width: 90, height: 120)

paymentsHistory = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
paymentsHistory.delegate = delegate
paymentsHistory.dataSource = dataSource
paymentsHistory.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")

self.view.addSubview(paymentsHistory)

Collection View 本身显示正确,但没有出现任何单元格。

我可以从调试输出中看到,

collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell

collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int

永远不会被调用。这是为什么?

最佳答案

我的猜测是它被取消引用了,因为 UICollectionView 中的 delegatedatasource 是弱引用。

weak open var delegate: UICollectionViewDelegate?
weak open var dataSource: UICollectionViewDataSource?

一旦你超出了你所在的任何函数的范围,它就应该设为 nil。

如果您将它作为您类(class)的属性(property),它应该可以工作。

关于ios - UICollectionView 不要求从 UICollectionViewDataSource 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44288765/

相关文章:

ios - 403 错误 - 这是一个错误。错误 : disallowed_useragent

ios - 自定义 Collection View 单元格添加图像

ios - 在 UICollectionView 中注册多个单元格(swift 3)

xcode - 在 Swift 字符串中插入换行符和回车符

swift - 快速多个弹出窗口

ios - Collection View 布局自身大小不起作用

ios - 具有分层类别的 UITableView -Swift iOS

ios - 如何以视觉语言格式编写内在大小的内容拥抱优先级?

ios - UiCollectionView 在保存对象时没有选择正确的单元格 Parse.com

ios - 是否可以从子类中推送 View Controller ?