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

UI基础:UICollectionView

2015-08-22 17:37 399 查看
UITableView 和 UICollectionView的设计思路:

1.UITableView 的布局由TableView和UItableViewDelegate完成.

2.UICollectionView布局样式由UICollectionViewFlowLayout和UICollectionViewDelegate完成.

数据源:

1.UITableView由UITableViewDataSource提供.

2.UICollectionView由UICollectionViewDataSource提供.

布局样式:

1.UITableView是多列单行.

2.UICollectionView是多行多列.

1.UITableViewCell上自带imageView.textLable.detailLabel.

2.UICollectionCell自带了contentView,contentView.

1.UItableViewCell创建时直接使用,后来使用了重用机制+注册.

2.UICollectionViewCell只有注册.

共同点:二者都是继承于UIScrollView,够可以滚.



UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];//初始化一个布局类CollectionView
layout.itemSize = CGSizeMake(150, 200);   //设置每个Item大小
layout.sectionInset = UIEdgeInsetsMake(20, 5, 10, 5); //设置每个item之间的间隙,上右下左
layout.minimumLineSpacing = 15;  //最小行间距
UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];//初始化一个CollectionView,并使用layout初始化

collectionView.delegate = self;//设置代理和dataSource
collectionView.dataSource = self;
[self.view addSubview:collectionView];
[collectionView release];
[layout release];

// 设置页眉的大小
layout.headerReferenceSize = CGSizeMake(320,40);
layout.footerReferenceSize = CGSizeMake(320, 20);

// 注册 Cell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionCell];

// 注册 页眉(header)
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kElementHeadView];
// 页脚(footer)
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kElementFootView];


代理UICollectionViewDataSource的协议中必须实现的两个方法:

// 返回每个分区里 item 的个数

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

}
//重用
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

}


// 点击 item 触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}
// 返回每个 item 的大小
- (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 {

}
// 返回 item 的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

}

// 返回页眉的 size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {

}

// 返回页脚的 size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {

}


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