UICollectionView: DataSource and Delegate
来源:互联网 发布:重庆美皇公司知乎 编辑:程序博客网 时间:2024/05/18 00:56
UICollectionView的数据源:
1.集合视图有多少个section?
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView { // _data is a class member variable that contains one array per section. return [_data count];}2.对应的section,有多少个items?
- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section { NSArray* sectionArray = [_data objectAtIndex:section]; return [sectionArray count];}
3.对应的section or items,显示什么样的view?
- 注册Cells 和 Supplementary Views
注册Cells:
registerClass:forCellWithReuseIdentifier:
registerNib:forCellWithReuseIdentifier:
注册Supplementary Views:
registerClass:forSupplementaryViewOfKind:withReuseIdentifier:
registerNib:forSupplementaryViewOfKind:withReuseIdentifier:
Supplementary Views需要指定一个额外的标示符“kind string”,布局对象负责定义它所支持的supplementary view的种类。例如,对于UICollectionViewFlowLayout类,支持两种supplementary view:一个section header view 和一个section footer view。为了标识这两种view,它定义了两个字符串常量:UICollectionElementKindSectionHeader 和 ICollectionElementKindSectionFooter。
注意:当你运用你自定义的布局时,需要负责定义自定义布局所支持的supplementary view。 - 复用和配置Cell 和 View
对于Cell,使用如下方法,配置Cell
collectionView:cellForItemAtIndexPath:
dequeueReusableCellWithReuseIdentifier:forIndexPath:
对于supplementary view,如下:
dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
collectionView:viewForSupplementaryElementOfKind:atIndexPath:- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MyCustomCell* newCell = [self.collectionView dequeueReusableCellWithReuseIdentifier:MyCellID forIndexPath:indexPath]; newCell.cellLabel.text = [NSString stringWithFormat:@"Section:%d, Item:%d", indexPath.section, indexPath.item]; return newCell;}
4.插入、删除、移动 section 和 Item?
插入,删除 or 移动 一个section or item,需遵循以下的步骤:
- 更新数据源里的数据。
- 调用collection view 对应的方法,来插入 or 删除 对应的section or item。
[self.collectionView performBatchUpdates:^{ NSArray* itemPaths = [self.collectionView indexPathsForSelectedItems]; // Delete the items from the data source. [self deleteItemsFromDataSourceAtIndexPaths:itemPaths]; // Now delete the items from the collection view. [self.collectionView deleteItemsAtIndexPaths:tempArray];} completion:nil];
5.管理选中or高亮状态
Collection View 支持单选,多选,还支持不可选。当一个cell的selectedBackgroundView的属性包含一个有效的view,collection View 就会在cell 选中 or 高亮时显示这个view。
UIView* backgroundView = [[UIView alloc] initWithFrame:self.bounds];backgroundView.backgroundColor = [UIColor redColor];self.backgroundView = backgroundView; UIView* selectedBGView = [[UIView alloc] initWithFrame:self.bounds];selectedBGView.backgroundColor = [UIColor whiteColor];self.selectedBackgroundView = selectedBGView;delegate 方法 提供如下的有关的方法:
collectionView:shouldSelectItemAtIndexPath:
collectionView:shouldDeselectItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
collectionView:didDeselectItemAtIndexPath:
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
collectionView:shouldDeselectItemAtIndexPath:
collectionView:didSelectItemAtIndexPath:
collectionView:didDeselectItemAtIndexPath:
collectionView:shouldHighlightItemAtIndexPath:
collectionView:didHighlightItemAtIndexPath:
collectionView:didUnhighlightItemAtIndexPath:
- (void)collectionView:(UICollectionView *)colView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor blueColor];} - (void)collectionView:(UICollectionView *)colView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell* cell = [colView cellForItemAtIndexPath:indexPath]; cell.contentView.backgroundColor = nil;}高亮状态是指用户的还在触摸设备时的状态。
选中状态是指触摸结束后,表示用户试图选中这个cell。
当在cell上长按时,collection view 会试图显示一个编辑菜单。编辑菜单能用来在collection view中剪切,复制和粘贴。在编辑菜单展示前,需按以下的步骤:
- 须实现以下三个协议来处理相关的动作:
collectionView:shouldShowMenuForItemAtIndexPath:
collectionView:canPerformAction:forItemAtIndexPath:withSender:
collectionView:performAction:forItemAtIndexPath:withSender: - 对指定的cell collectionView:shouldShowMenuForItemAtIndexPath: 方法,须返回YES
collectionView:canPerformAction:forItemAtIndexPath:withSender: 方法对于至少一个期望的动作必须返回YES。collection view 支持的动作有:cut:copy:paste:
collection view 调用collectionView:performAction:forItemAtIndexPath:withSender: 方法,执行对应的动作
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender { // Support only copying and pasting of cells. if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"]) return YES; // Prevent all other actions. return NO;}
7.布局间的过渡
布局间过渡最简单地方法是使用 setCollectionViewLayout:animated:方法,然而,如果你要控制过渡的过程,或者想让它可以交互,就需要使用UICollectionViewTransitionLayout对象。
使用UICollectionViewTransitionLayout对象的步骤如下:
- 使用initWithCurrentLayout:nextLayout:方法创建类
- 周期性的修改transitionProgress属性的值,在改变过渡的进度后,使用collection view的invalidateLayout方法,使布局无效。
- collectionView:transitionLayoutForOldLayout:newLayout: 代理方法,返回过渡的布局对象。
- 选择性的使用updateValue:forAnimatedKey:方法修改布局对象的值,稳定值为0。
0 0
- UICollectionView: DataSource and Delegate
- (作业)TableView、Delegate、DataSource
- Datasource 与 Delegate的对比
- UITableView中的DataSource和delegate
- tableview中的delegate、DataSource原理
- Core Plot中DataSource和Delegate
- UITableView的数据源(dataSource)和代理(delegate)
- 分离 UITableView 的 Delegate 和 Datasource
- datasource and driverManager
- sparksql and datasource
- iOS exception 'NSInternalInconsistencyException', reason: 'UICollectionView dataSource is not set'
- Differences between Datasource and XADatasource
- DELEGATE AND EVENT
- Delegate and Evant
- Delegate and Protocol
- About Delegate and Event
- Protocol and Delegate
- Block Delegate and Closure
- AsyncQueryHandler学习 (转)
- iOS应用程序安全(1)-搭建移动渗透测试平台
- cocos2d-x 场景动画切换过渡大全
- Android开源项目第五篇——优秀个人和团体篇
- C语言存储类型
- UICollectionView: DataSource and Delegate
- 指针函数与函数指针的区别
- poj 2375 BFS实现强连通分量缩点
- 话说win8是不是太烂了点
- Android 手把手教您自定义ViewGroup(一)
- android--自定义ProgressDialog显示位置(其它Dialog子类都可以设置)
- Objective-C NSArray
- HDU 4924 Football Manager DP
- 百度语音识别API使用小结