UICollectionView基础
2016-04-03 22:23
351 查看
前言
这个控件,看起来与UITableView有点像,而且基本的用法也很相像哦!!!我们来看看API:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #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; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #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); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #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__); } |
相关文章推荐
- lintcode:Unique Binary Search Trees II
- DuiLib(5)——CWindowWnd窗口函数简介
- [Queue]——面向接口编程
- 【POJ2926】Requirements【二进制】【最大曼哈顿距离】
- String、StringBuffer和StringBuilder
- lintcode: Unique Binary Search Trees
- [html][LigerUI]使用示例
- DELL DOSA 6.X 服务器引导光盘Dell Systems Build and Update Utility ISO下载地址
- IOS9 UIStackView简介
- DuiLib(4)——消息响应
- Android 入门三 UI界面
- 02-线性结构3 Pop Sequence
- request 和 response之间实现servlet线程非阻塞
- Liferay 6.1开发学习(四):Service Builder
- Android入门(五)UI-单位与尺寸、ListView
- Android入门(四)UI-创建自定义控件
- iOS UIVisualEffectView 为视图添加特殊毛玻璃的效果
- Providing Resources
- UI基础 - UINavigationController
- Vector , list 和 deque的区别