UICollectionView的基本使用
来源:互联网 发布:小米3能不能用4g网络 编辑:程序博客网 时间:2024/05/24 06:08
这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!!
我们来看看API:
- #pragma mark - UICollectionViewDataSource
- // 指定Section个数
- - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
- return 3;
- }
- // 指定section中的collectionViewCell的个数
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return 10;
- }
- // 配置section中的collectionViewCell的显示
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellIdentifier" forIndexPath:indexPath];
- cell.backgroundColor = [UIColor redColor];
- cell.textLabel.text = [NSString stringWithFormat:@"(%ld %ld)", indexPath.section, indexPath.row];
- return cell;
- }
看看直线布局的API:
- #pragma mark - UICollectionViewDelegateFlowLayout
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- return CGSizeMake(self.view.frame.size.width / 3 - 10, self.view.frame.size.width / 3 - 10);
- }
- // 设置每个cell上下左右相距
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
- return UIEdgeInsetsMake(5, 5, 5, 5);
- }
- // 设置最小行间距,也就是前一行与后一行的中间最小间隔
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
- return 10;
- }
- // 设置最小列间距,也就是左行与右一行的中间最小间隔
- - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
- return 10;
- }
- // 设置section头视图的参考大小,与tableheaderview类似
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
- return CGSizeMake(self.view.frame.size.width, 40);
- }
- // 设置section尾视图的参考大小,与tablefooterview类似
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
- return CGSizeMake(self.view.frame.size.width, 40);
- }
如果是固定的,可以使用全局属性:
- NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewFlowLayout : UICollectionViewLayout
- @property (nonatomic) CGFloat minimumLineSpacing;
- @property (nonatomic) CGFloat minimumInteritemSpacing;
- @property (nonatomic) CGSize itemSize;
- @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -perferredLayoutAttributesFittingAttributes:
- @property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
- @property (nonatomic) CGSize headerReferenceSize;
- @property (nonatomic) CGSize footerReferenceSize;
- @property (nonatomic) UIEdgeInsets sectionInset;
- @end
常用到的代理方法:
- #pragma mark - UICollectionViewDelegate
- // 允许选中时,高亮
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
- // 高亮完成后回调
- - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
- // 由高亮转成非高亮完成时的回调
- - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
- // 设置是否允许选中
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
- // 设置是否允许取消选中
- - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- return YES;
- }
- // 选中操作
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
- // 取消选中操作
- - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
- NSLog(@"%s", __FUNCTION__);
- }
demo:https://github.com/632840804/CollectionViewDemo
0 0
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- uicollectionview的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- UICollectionView的基本使用
- 56.UICollectionView的基本使用
- UICollectionView的基本使用(一)
- UICollectionView的基本使用(1)
- iOS 心得二 UICollectionView的基本使用
- UICollectionView的基本使用完整版(包括UICollectionReusableView)
- iOS-UICollectionView基本使用
- UICollectionView的基本用法
- UICollectionView的基本用法
- Linux下Signal信号
- 第十四周 项目2-二叉树排序树中查找的路径
- 第十六周项目1-验证算法(6)堆排序
- 第十周实践项目1 - 二叉树算法库
- 第十五周项目5—希尔排序
- UICollectionView的基本使用
- xcode注释快捷键
- redis专题-1.Redis简介
- C#接口作用的深入理解
- jQuery移除或禁用html元素的点击事件
- 第16周项目1-验证算法(4)快速排序
- 第十四周 项目 1 - 验证算法之线性表的折半查找
- 第十六周项目一(5)归并排序
- 小米5s安装xpose 下