UICollectionView使用
来源:互联网 发布:淘宝助手安卓版 编辑:程序博客网 时间:2024/06/05 05:44
一、Storyboard的准备
1.在storyboard的ViewControll拖一个CollectionView
2.在collectionViewCell中添加ImageView和Label
3.创建一个UICollectionViewCell的类,并跟上面的collectionCell绑定
4.连线dataSource和delegate,并创建映射
imageView 、titleLabel
二、代码:
1.
//创建一个可变数组 self.dataMArr = [NSMutableArray array]; //创建9张图显示,并加其对应的titleLabel加入字典 for (NSInteger index = 0; index < 9; index++) { UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld", (long)index+1]]; NSString *title = [NSString stringWithFormat:@"{0,%ld}",(long)index+1]; NSDictionary *dic = @{@"image": image, @"title": title}; [self.dataMArr addObject:dic]; } /**记得要遵从协议UICollectionViewDelegate, UICollectionViewDataSource */ self.myCollection.delegate = self; self.myCollection.dataSource = self;
2.在.m文件中实现代理方法
//collectionView item的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataMArr.count;}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //myCollectionCell 这个CellID需要在storyboard那里对应添加相同的,如图三 static NSString *collectionCellID = @"myCollectionCell"; CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:collectionCellID forIndexPath:indexPath]; NSDictionary *dic = self.dataMArr[indexPath.row]; UIImage *image = dic[@"image"]; NSString *title = dic[@"title"]; cell.imageView.image = image; cell.titleLabel.text = title; return cell;}
图三
0 0
- UICollectionView使用
- UICollectionView 使用
- UICollectionView使用
- UICollectionView使用
- IOS6- UICollectionView的使用
- UICollectionView 自定义使用
- ios UICollectionView的使用
- UICollectionView 使用 介绍
- UICollectionView的简单使用
- UICollectionView 使用 介绍
- UICollectionView 自定义使用
- UICollectionView 的使用
- UICollectionView使用 - iOS开发
- UICollectionView 使用 介绍
- 使用Xib创建UICollectionView
- UICollectionView的使用
- UICollectionView的使用
- ios UICollectionView的使用
- new和newInstance() Constructor.newInstance()区别
- Django Form源码分析之Field验证逻辑
- GIT 添加ssh key
- html中的css样式
- HTML标签元素的分类
- UICollectionView使用
- IOS单元测试(—)
- netstat
- iOS-利用运行时给分类添加属性
- 数字推理技巧
- PHP 生成excel|好用强大的php excel类库
- Vim的分割窗口split命令
- 内存是手游的硬伤——Unity游戏Mono内存管理与泄漏
- hiho一下 第109周:Tower Defense Game