您的位置:首页 > 产品设计 > UI/UE

UICollectionView使用过程中的常见错误

2017-09-17 16:59 260 查看
UITableView和UICollectionView是在IOS开发过程中使用得比较多的数据展示控件。这两个控件各有千秋,有各自的最适应场合。UICollectionView在使用的时候比UITableView更严格,特别是侧重于UICollectionViewCell的重用,在创建诸多对象的时候,一定要使用重用机制,否则就会报相应 的错误,下面就对这些常见错误做一个总结:

1.reason:'the cell returned from -collectionView:cellForItemAtIndexPath: does not have a reuseIdentifier。 cells must be retrieved by calling-dequeueReusableCellWithReuseIdentifier:forIndexPath:'说明cell必须使用dequeueReusableCellWithReuseIdentifier:forIndexPath这个方法进行创建,也就意味着我们必须设置cell的重用标识

2.reason: 'could not dequeue a view of kind: UICollectionElementKindCell withidentifier *** - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'--没有注册cell,也就意味着系统[b]不知道按那种方式来进行cell的创建。我们可以使用storyboard注册,xib注册和class注册。[/b]

3. reason:'UICollectionView must be initialized with a non-nil layout parameter':必须指定'UICollectionView的布局对象,如果没有,则不能使用其进行布局.那么就意味着我们必须传入一个布局对象,也就意味着只能通过[[UICollectionView alloc] initWithFrame:<#(CGRect)#> collectionViewLayout:<#(nonnull
UICollectionViewLayout *)#>];来创建UICollectionView控件

4.创建好UICollectionView对象后,没有看到预期的效果。程序不运行创建cell的方法:这是因为我们创建了UICollectionViewLayout,它里面没有任何的布局属性,也就意味着系统会认为每一个cell的大小都是0,那么就不会创建cell.所以我们需要给其创建一个能够进行具体布局属性设置的UICollectionViewFlowLayout,并在其中设置好属性

5. reason:'invalid nib registered for identifier (***) - nib must contain exactly one top level object which must be a UICollectionReusableView instance':在xib文件中只能定义一个顶级视图,简单说就是只能定义一个cell.如果定义了多个就会报这个错误。

看懂了这几种错误,在使用UICollectionView就会顺利很多啦。

作者:wuhu0723

链接:http://www.jianshu.com/p/1a21311112a7

來源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: