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

iOS开发:集合视图 UICollectionView

2015-09-26 14:29 543 查看
iOS:集合视图 UICollectionView
1.创建集合视图的步骤:

(1).使用系统的布局UICollectionViewFlowLayout

//初始化标准瀑布流的对象
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//每一块的大小
flowLayout.itemSize = CGSizeMake(355, 655);
//最小行间距
flowLayout.minimumLineSpacing = 15;
//最小列间距
flowLayout.minimumInteritemSpacing = 15;
//设置上左下右的距离
flowLayout.sectionInset = UIEdgeInsetsMake(20, 15, 20, 15); //整个collectionView距离上左下右
//滚动方向(默认是纵向vertical)
flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
(2).设置代理,设置数据源

<UICollectionViewDataSource, UICollectionViewDelegate>
//初始化CollectionView

_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];
_collectionView.dataSource = self;

_collectionView.delegate = self;
//整页滚动
_collectionView.pagingEnabled = YES;
[self.view addSubview:_collectionView];
(3).设置自定义cell

#warning 重点:collectionView 注册cell
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
//实现协议方法:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return _dataSourceArray.count; //返回块数
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

//直接进重用池找可用cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 225.0 green:arc4random() % 256 / 225.0 blue:arc4random()
% 256 / 225.0 alpha:1];
return cell;
}

//获取当前cell的下标

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath{

NSLog(@"%ld", indexPath.row);
}

(4).设置 <UICollectionViewDelegateFlowLayout>代理

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>
@optional

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath
*)indexPath;

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
insetForSectionAtIndex:(NSInteger)section;

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: