day15---UICollectionView

1.1 概念
UICollectionView是多行多列的形式下显示数据的一种控件,从UITableView演变而来,所以使用上大致与UITableView一样,但是比UITableView更加灵活,实现的效果更多样化;

1.2 UICollectionView与UITableView的异同点:
a 相同点:
UITableView:
.dataSource
.delegate
加载数据:三问(有几个分区?每个分区多少行?每行什么内容?)
响应用户:一答(选中某行后响应)
每个分区都有自己的头部视图,尾部视图;

    UICollectionView:
        .dataSource
        .delegate
    加载数据:三问(有几个分区?每个分区有多少项?每项显示什么内容?)
    响应用户:一答(选中某项后响应)
    每个分区有自己的头部,尾部视图;



b 不同点:
    1)  UICollectionView是以多行多列的形式展现数据
            UITableView是以单行单列的形式展现数据

    2)  UITableView只能默认是上下滚动
        UICollectionView即可以上下滚,也可以左右滚;
    
    3)  UITableViewCell 自带系统的四个样式,并且还提供了三种控件(头像,主标题,副标题)
        UICollectionViewCell 没有任何系统自定义好的样式,也没有系统提供的任何子控件,只提供了三个属性:设置背景颜色,设置背景视图,contentView; 

                                            【** 最大区别 **】
    4)  UITableView的每一行,在设定好行高以后,排布位置就固定了;
        UICollectionView需要明确的使用一个“布局对象”来对其中包含的所有向进行定位,其中布局对象,是从UICollectionViewLayout派生出来的子类对象,系统给我们默认提供了一个流式布局“UICollectionViewFlowLayout”,继承自UICollectionViewLayout; 
                        
【Demo】-【1-UICollectionView】                   
  • (void)viewDidLoad {
    [super viewDidLoad];

    //1.请求数据
    [self reloadData];

//2.创建UICollectionView
[self creatCollectionView];

}

-(void)reloadData
{
self.dataArray = [[MyModel requestData] mutableCopy];
}

-(void)creatCollectionView
{
self.automaticallyAdjustsScrollViewInsets = NO;

//流式布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//***************这只流式布局的相关属性
//1.单元格(项)的大小                           流式布局单元格大小决定了每行有几个项
layout.itemSize = CGSizeMake((screenBounds.size.width-60)/3, 180);
//2.每个单元格之间最小的间距
layout.minimumInteritemSpacing = 10;
//3.每行之间的最小间距
layout.minimumLineSpacing = 10;
//4.设置每个分区之间的边距
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
//5.设置滚动的方式(横向滚动/竖向滚动)
 collectionView即可以横向滚动,也可以竖向滚动
layout.scrollDirection = UICollectionViewScrollDirectionVertical;




//1.创建collectionView对象
UICollectionView *cView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, screenBounds.size.width, screenBounds.size.height-64) collectionViewLayout:layout];
cView.delegate = self;
cView.dataSource = self;
cView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:cView];

//提前注册cell
[cView registerClass:[MyCell class] forCellWithReuseIdentifier:@"Cell"];

//提前注册,头部/尾部视图
//第二个参数:表示注册的是头部视图?还是尾部视图?
[cView registerClass:[SectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
[cView registerClass:[SectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];

}

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

pragma mark -UICollectionViewDataSource

//1问 有多少个分区
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return self.dataArray.count;
}

//2问 每个分区有多少项
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [self.dataArray[section] count];
}

//3问 每一项显示什么内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//从复用池中取有没有符合要求的单元格,如果有,直接返回使用,如果没有,系统会根据之前注册的单元格,自动的创建一个单元格对象,并返回;
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
//因为UICollectionViewCell是系统自带的cell,所以没有提供样式,没有提供对应的空间,只能设置相关属性;
cell.model = self.dataArray[indexPath.section][indexPath.row];

cell.backgroundColor = [UIColor orangeColor];


return cell;

}

//如果要给UICollectionView添加分区的头部,尾部视图
//注意:
//1.分区的头部,尾部视图必须是UICollectionReusableView类型的对象
//2.提前先注册头部,尾部视图的类型
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//需要返回的是头部视图
//从复用池取出对应的头部视图
SectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
headerView.myLabel.text = [NSString stringWithFormat:@"我是第%ld分区的头部视图",indexPath.section+1];
return headerView;
}
//没有尾部视图,直接返回nil;
else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
{
SectionHeaderView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Footer" forIndexPath:indexPath];
footerView.myLabel.text = [NSString stringWithFormat:@"我是第%ld分区的尾部视图",indexPath.section+1];
return footerView;
}
return nil;
}

pragma mark -UICollectionViewDelegateFlowLayout

                                【** collectionView的头部尾部视图设置方法 **】

//设置头部视图的高度
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(collectionView.frame.size.width, 50);

}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
return CGSizeMake(collectionView.frame.size.width, 30);
}

pragma mark -UICollectionViewDelegate

//当选中某一个单元格,则相应
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
DetailViewController *detailView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
detailView.model = self.dataArray[indexPath.section][indexPath.row];

detailView.block = ^(NSString *title){
    MyModel *model = self.dataArray[indexPath.section][indexPath.row];
    model.title = title;
    [collectionView reloadData]; 
};

[self.navigationController pushViewController:detailView animated:YES];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 198,932评论 5 466
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,554评论 2 375
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 145,894评论 0 328
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,442评论 1 268
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,347评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 47,899评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,325评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,980评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,196评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,163评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,085评论 1 328
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,826评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,389评论 3 302
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,501评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,753评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,171评论 2 344
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,616评论 2 339

推荐阅读更多精彩内容

  • UITableView 表格视图一 UITableView1.1是什么?以列表的方式展示数据的一种控件,且继承自...
    037e3257fa3b阅读 241评论 0 1
  • 一、UICollectionView介绍 UICollectionView和UICollectionViewCon...
    无沣阅读 4,455评论 4 18
  • 近代以来,中国传统文化在西方文化的强烈冲击下,正在不断流失,令人堪忧。 昨天晚上,看到一个小视频,就是类似于过万圣...
    上善若水_张建国阅读 388评论 0 0
  • 昨天下午第一次给这个学生上课,晚上微信就收到了妈妈这样的反馈,早上妈妈打电话过来,学生说从此喜欢上了数学。这实在是...
    Ellis方阅读 228评论 0 0
  • 结果: enumerate还可以接收第二个参数,用于指定索引起始值,如: 结果:
    SingleDiego阅读 246评论 0 0