CollectionViewCell 不执行 cellForItemAtIndexPath 方法的问

针对在网络上说 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.

  1. If you are using Flow layout: You need to set the item sizes properly.

  2. 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 了。



转载于:https://my.oschina.net/RainyZou/blog/537838

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值