我已经将自定义数据源设置为 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 中的 delegate
和 datasource
是弱引用。
weak open var delegate: UICollectionViewDelegate?
weak open var dataSource: UICollectionViewDataSource?
一旦你超出了你所在的任何函数的范围,它就应该设为 nil。
如果您将它作为您类(class)的属性(property),它应该可以工作。
关于ios - UICollectionView 不要求从 UICollectionViewDataSource 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44288765/