针对在网络上说 cellForItemAtIndexPath不执行的问题,网上有很多说,但是发现不是过时就是不对,刚好今天我解决了改问题,所以写下来备注。
首先我像使用UITableViewCell一样实现 Datasource, delegate 二个函数,但是不同于 tableview , collectionView 需要设置一个 UICollectionViewLayout. Layout 需要设置Item 相应的 size. 如英文引入:
The cellForItemAtIndexPath will not get called if you do not provide the content size information to collection view.
If you are using Flow layout: You need to set the item sizes properly.
If you have a custom layout subclassed from UICollectionViewLayout: Ensure you are returning a proper size from the collectionViewContentSize method.
所以在我们使用 UICollectionViewFlowLayout 初始化 UICollectionView 的时候,那么我们需要在设置 delegate 的controller中同时实现 -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
这样就可以正常显示 Cell 了。