1.注册:
//3. 代码方式向系统注册ID类型的单元格
[tabView registerClass:[MyTableViewCell class] forCellReuseIdentifier:ID];
//3.1 nib方式向系统注册ID类型的单元格
[tabView registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
2. UICollectionView创建
//1.创建布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//1.1. 设置最小行间距
layout.minimumLineSpacing = 20;
//1.2. 设置最小表格间距
layout.minimumInteritemSpacing = 10;
//1.3. 设置单元格大小
layout.itemSize = CGSizeMake(90, 90);
//1.4. 设置横向滑动,默认垂直
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
/*UICollectionViewScrollDirectionVertical,
UICollectionViewScrollDirectionHorizontal*/
//2.2. 设置头视图的大小
layOut.headerReferenceSize = CGSizeMake(375, 40);
//2.创建collection
UICollectionView *collection = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
[self.view addSubview:collection];
collection.dataSource = self;
collection.delegate = self;
/**设置减速方式*/
self.decelerationRate = UIScrollViewDecelerationRateFast;
static NSString *ID = @"cell";
//向系统注册这个类型的单元格
[collection registerClass:[MyCell class] forCellWithReuseIdentifier:ID];
//3.复写代理方法 < UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
//返回多少个单元格
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 29;
}
//创建单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
cell.imgName = [NSString stringWithFormat:@"%ld@2x.png",indexPath.row+1];
return cell;
}
//设置单元格的显示位置
[collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
3.UICollectionView代理方法
//1. 返回多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//2. 创建组的头视图
//2.1. 定义ID
static NSString *header = @"cellHead";
//2.2. 设置头视图的大小
flowLayOut.headerReferenceSize = CGSizeMake(375, 40);
//2.3. 注册头视图
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];
//2.4. 复写组头视图方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
//2.4.1 取得头视图
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header forIndexPath:indexPath];
//2.4.2 返回头视图
return headerView;
}
//3. 设置每一个单元格的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGFloat num = arc4random_uniform(50)+50;
return CGSizeMake(num, num);
}
//4. 设置每一组视图的停靠位置
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
// CGFloat top, left, bottom, right;
UIEdgeInsets edge = UIEdgeInsetsMake(20, 20, 20, 20);
return edge;
}
//5. 点击单元格调用的代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//6.cell结束显示的时候调用的 /**当collection单元格滑出屏幕后 恢复尺寸*/
/**当collection单元格滑出屏幕后 恢复尺寸*/
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
/*
层次为:一个视图控制器 -> 上面一个collectionview* -> collectionview的单元格上一个scrollview
-> scrollview上面一个imageView
*/
/**在collection的代理方法中获取到滑动视图*/
PhotoCollectionViewCell *pCell = (PhotoCollectionViewCell *)cell;
PhotoScrollView *sc = pCell.scrollView;
[sc setZoomScale:1.0 animated:YES];
}
22-UICollectionView
最新推荐文章于 2020-12-23 22:19:26 发布