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

UICollectionView的用法小结

2015-12-11 17:16 381 查看
UICollectionView的简单用法,具体可参考/article/8033855.html,有相关的三篇博客,个人感觉还不错,以下是个人的简单理解与应用,可以满足最简单的没有什么特殊展现形式的UICollectionView,后期鄙人也会不断的学习完善,以下是代码有Demo完整的展示,并且都附有注释详解,应该会很容易理解的,希望对各位有帮助,有啥不足的地方或者意见、建议,真心希望留言,咱们共同学习,一起进步,谢谢!

//
//  ViewController.m
//  UICollertionViewDemo
//
//  Created by Red_Coral on 15/7/30.
//  Copyright (c) 2015年 Red_Coral. All rights reserved.
//

#import "ViewController.h"

#define COLLECTIONVIEW_HEADERSECTION_IDENTIFIER @"collectionViewHeaderSctionView_indentifier"
#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

flowLayout.headerReferenceSize = CGSizeMake([[UIScreen mainScreen] bounds].size.width,100);
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-2) collectionViewLayout:flowLayout];
//这侧UICollectionViewCell(此处也可以自定义的cell类),但是后面的identifier和下面引用的必须要一致
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell_identifier"];
//注册UICollectionView的section的Header或者footer,同样也要注意identifier的一致性
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:COLLECTIONVIEW_HEADERSECTION_IDENTIFIER];
collectionView.backgroundColor = [UIColor redColor];
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.shouldGroupAccessibilityChildren = YES;
[self.view addSubview:collectionView];

}
//确定UICollectionView每个章节有几个元素cell,
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 10;
}
//确定UICollectionView有几个章节,(这个方法可以省略)默认是一个
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 2;
}
//制定每个cell将要在试图上展线成的样式及其内容
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *tempStr = @"UICollectionViewCell_identifier";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:tempStr forIndexPath:indexPath];
[cell sizeToFit];
if (!cell) {
NSLog(@"无法创建CollectionViewCell时打印,自定义的cell就不可能进来了。");
}
cell.backgroundColor = [UIColor greenColor];

return cell;
}
/*
*1、此处为Supplementary Views 追加视图 (类似Header或者Footer)
*2、用到的identifier需要和注册的一致(可在此处添加自定义)
*/
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

NSLog(@"kind: %@",kind);

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
UICollectionElementKindSectionHeader withReuseIdentifier:COLLECTIONVIEW_HEADERSECTION_IDENTIFIER forIndexPath:indexPath];
headerView.backgroundColor = [UIColor yellowColor];
return headerView;
}
//确定每个cell的宽和高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
//cell的宽、高
return CGSizeMake((SCREEN_WIDTH-3)/4., (SCREEN_WIDTH-3)/4.);
}
//定义每个UICollectionView的cell的layout情况,ps:你可以将下面的值意义改变,看看效果
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(10, 0, 0, 0);
}

//定义每个UICollectionView items列与列之间的间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

return 1;
}
//items行与行之间的间隔
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1;
}

#pragma mark - UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

NSLog(@"选择第%ld章%ld个cell",(long)indexPath.section,(long)indexPath.row);
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];

}

@end


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