使用collectionView的方法:
代碼如下:
主要的是這三步:
1没炒、創(chuàng)建layout
2艰额、根據(jù)layout創(chuàng)建collectionView
3陶因、設(shè)置代理
// 1堰汉、創(chuàng)建layout
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 100; // 行距
layout.minimumInteritemSpacing = 10; // 列距
layout.itemSize=CGSizeMake((ScreeFrame.size.width - 40) / 3, (ScreeFrame.size.height - 80) / 4);
layout.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
// 2、根據(jù)layout創(chuàng)建collectionView
self.collectionView_Subject = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 10, ScreeFrame.size.width, ScreeFrame.size.height - 64) collectionViewLayout:layout];
// 3费什、注冊(cè)item 和 header Footer (根據(jù)用戶需求)
[self.collectionView_Subject registerNib:[UINib nibWithNibName:@"SubjectCollectionViewCell" bundle:nil]forCellWithReuseIdentifier:@"SubjectId"];
[self.collectionView_Subject registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header_id"];
[self.collectionView_Subject registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer_id"];
// 4钾恢、設(shè)置代理
self.collectionView_Subject.delegate = self;
self.collectionView_Subject.dataSource = self;
self.collectionView_Subject.backgroundColor = [UIColor whiteColor];
[self addSubview:self.collectionView_Subject];
集合視圖的代理就和tableView類似了:設(shè)置sections、rows吕喘、cell赘那、以及頭/尾視圖。
1> 設(shè)置item大新戎省:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
2> 設(shè)置頭視圖和尾視圖
/**
* 根據(jù)樣式判斷是頭還是尾
* UICollectionElementKindSectionFooter
* UICollectionElementKindSectionHeader
*/
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
// 頭視圖大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(ScreeFrame.size.width, ScreeFrame.size.height / 12);
}
// 尾視圖大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
return CGSizeMake(ScreeFrame.size.width, 10);
}