转载:UICollectionReusableView的用法
2015-09-23 15:32
513 查看
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。
使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。
下面给出一些常用方法,具体的使用可以参考Demo:点我下载
[objc] view
plaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"UICollectionView学习";
//通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell
[self.collectionView registerNib:[UINib nibWithNibName:@"SQCollectionCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];
//注册headerView Nib的view需要继承UICollectionReusableView
[self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];
//注册footerView Nib的view需要继承UICollectionReusableView
[self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];
//
self.collectionView.allowsMultipleSelection = YES;//默认为NO,是否可以多选
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -CollectionView datasource
//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 6;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//重用cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];
//赋值
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UILabel *label = (UILabel *)[cell viewWithTag:2];
NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];
imageView.image = [UIImage imageNamed:imageName];
label.text = imageName;
cell.backgroundColor = [UIColor redColor];
return cell;
}
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
NSString *reuseIdentifier;
if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
reuseIdentifier = kfooterIdentifier;
}else{
reuseIdentifier = kheaderIdentifier;
}
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[view viewWithTag:1];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
label.text = [NSString stringWithFormat:@"这是header:%d",indexPath.section];
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
view.backgroundColor = [UIColor lightGrayColor];
label.text = [NSString stringWithFormat:@"这是footer:%d",indexPath.section];
}
return view;
}
//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(60, 80);
}
//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右
}
//返回头headerView的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size={320,45};
return size;
}
//返回头footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
CGSize size={320,45};
return size;
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//每个item之间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
// return 100;
//}
//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor greenColor]];
}
//取消选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
}
效果图如下:
内容来源:http://blog.csdn.net/Apple_app/article/details/38867123
使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。
下面给出一些常用方法,具体的使用可以参考Demo:点我下载
苹果官方Demo:点我下载
[objc] viewplaincopy
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"UICollectionView学习";
//通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell
[self.collectionView registerNib:[UINib nibWithNibName:@"SQCollectionCell" bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];
//注册headerView Nib的view需要继承UICollectionReusableView
[self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];
//注册footerView Nib的view需要继承UICollectionReusableView
[self.collectionView registerNib:[UINib nibWithNibName:@"SQSupplementaryView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];
//
self.collectionView.allowsMultipleSelection = YES;//默认为NO,是否可以多选
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -CollectionView datasource
//section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 6;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//重用cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];
//赋值
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UILabel *label = (UILabel *)[cell viewWithTag:2];
NSString *imageName = [NSString stringWithFormat:@"%ld.JPG",(long)indexPath.row];
imageView.image = [UIImage imageNamed:imageName];
label.text = imageName;
cell.backgroundColor = [UIColor redColor];
return cell;
}
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
NSString *reuseIdentifier;
if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
reuseIdentifier = kfooterIdentifier;
}else{
reuseIdentifier = kheaderIdentifier;
}
UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind :kind withReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UILabel *label = (UILabel *)[view viewWithTag:1];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
label.text = [NSString stringWithFormat:@"这是header:%d",indexPath.section];
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
view.backgroundColor = [UIColor lightGrayColor];
label.text = [NSString stringWithFormat:@"这是footer:%d",indexPath.section];
}
return view;
}
//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(60, 80);
}
//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右
}
//返回头headerView的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size={320,45};
return size;
}
//返回头footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
CGSize size={320,45};
return size;
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
return 10;
}
//每个item之间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
//{
// return 100;
//}
//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor greenColor]];
}
//取消选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
}
效果图如下:
内容来源:http://blog.csdn.net/Apple_app/article/details/38867123
相关文章推荐
- iOS-Core-Animation-Advanced-Techniques(四-1)
- Android samples API Demos之UI篇1(ActionBarCompat-Styled)
- [AFNetworking]源代码分析--AFURLRequestSerialization.h
- autolayout之后获取uiview的frame
- easyui datagrid shift 多选
- UITextField通过UIPickerView显示选择内容
- UIImageView的属性之animationImages详解
- UIKit框架的介绍
- iOS学习之UISwitch控件两种使用方法和监听
- UIAlertView使用全解
- Android布局属性android:clipToPadding的UI设计妙用
- 可以自定义截屏大小的截屏功能
- easyUI中使用datatimebox选定开始日期时间后自动带出7天之后的结束日期时间
- UI中TExtField的几种回收键盘的方法
- UISegmentedControl的所有操作总结
- Iphone开发之UIView中的动画属性
- KVC中setValuesForKeysWithDictionary:详解
- java的String类StringBuffer类StringBuilder类
- UITabBarController使用详解
- ios开发——长按按出两个UIAlertView的解决办法