关于创建UICollectionView的几种方法的总结
1、用storyBoard方式创建,在storyBoard中拖进去一个UICollectionView,然后将它与h文件连接,向UICollectionView中拖一个UICollectionViewCell,然后设置cell的显示格式,注意必须设置cell的resuseID(不是RestorationID),在m文件中不需要注册一个cell直接在代理方法中使用代码为:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
2、用UICollectionViewCell.xib创建cell,在视图控制器中注册cell,注意在这里必须要标明cell的重用id,不然后出现错误,标明重用ID时和上面内容一样。
主要代码如下:
//注册一个collectionView
[_collectionView registerClass:[FirstCell class] forCellWithReuseIdentifier:@"list"];
//重用单元格
FirstCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list" forIndexPath:indexPath];
3、代码创建
主要代码如下:
//创建collcetionView
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height) collectionViewLayout:flowLayout];
//注册一个collectionView
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
//重用单元格
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
目前我理解的创建方式有这几种,以后会继续更新,谢谢支持!
自己创建的几种方式的代码链接为:
http://note.youdao.com/yws/public/redirect/share?id=bf117f4cad9b8ce0c73bfa3de78f2e1d&type=false