UICollectionView的使用方法
1、遵守協議
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
2、創建
?UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
??? layout.minimumInteritemSpacing = 10;? //最小item之間的間距
??? layout.minimumLineSpacing = 10;//最小行間距
??? collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64-49) collectionViewLayout:layout];
??? collectionView.delegate = self;
??? collectionView.dataSource = self;
//重要
1>、如果是用代碼自定義的cell要用下面的方法注冊
?[collectionView registerClass:[PicCollectionViewCell class] forCellWithReuseIdentifier:@"cc"];
2>、如果是用xib定義的cell要用
[collectionView registerNib:[UINib nibWithNibName:@"PicCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cc"];
3、返回item的個數
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
??? return dataSourse.count;
}
4、cell復用
xib和代碼都用下面方法
PicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cc" forIndexPath:indexPath];
5、重要協議方法
1>返回item的大小,系統自動根據item的大小來設定每行顯示的item個數(可以用layout.size方法)
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
??? CGSize? size = CGSizeMake(90, 80);
??? return size;
}
2>//返回這個UICollectionView是否可以被選擇?
-(BOOL)collectionView:(UICollectionView?*)collectionView?shouldSelectItemAtIndexPath:(NSIndexPath?*)indexPath??
{? return?YES;??
}
轉載于:https://www.cnblogs.com/huoxingdeguoguo/p/4630948.html
總結
以上是生活随笔為你收集整理的UICollectionView的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: “三月尽是头白日”下一句是什么
- 下一篇: 整形双眼皮多少钱啊?