iOS---collectionView
来源:互联网 发布:java运维实施岗位职责 编辑:程序博客网 时间:2024/05/16 08:12
转载自:http://m.ithao123.cn/content-1001849.html
iOS CollectionView的出现是一大福利,再也不用用TableView来定义复杂的多栏表格了,用法与Table类似,只是Cell必须自己添加,无默认模式
由于CollectionView没有默认的Cell布局,所以一般还是自定义方便又快捷
一、自定义Cell
1、新建类CollectionCell继承自UICollectionViewCell
2、新建Xib,命名为CollectionCell.xib
a.选中CollectionCell.xib删掉默认的View,从控件中拖一个Collection View Cell(图3)到画布中,设置大小为95*116;
b.选中刚刚添加的Cell,更改类名为CollectionCell,如图4
c.在CollectionCell.xib的CollectionCell中添加一个ImageView和一个Label(图5)
d.创建映射, 图6,图7
e.选中CollectionCell.m , 重写init方法
01
- (id)initWithFrame:(CGRect)frame
02
{
03
self = [super initWithFrame:frame];
04
if
(self) {
05
// 在此添加
06
07
// 初始化时加载collectionCell.xib文件
08
NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@
"CollectionCell"
owner:self options: nil];
09
10
// 如果路径不存在,return nil
11
if
(arrayOfViews.count < 1){
return
nil;}
12
13
// 如果xib中view不属于UICollectionViewCell类,return nil
14
if
(![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell
class
]]){
15
return
nil;
16
}
17
18
// 加载nib
19
self = [arrayOfViews objectAtIndex:0];
20
}
21
return
self;
22
}
1、拖动一个Collection View到指定ViewController的View上
2、连线dataSource和delegate,并创建映射,命名为CollectionView
3、选中CollectionView的标尺,将Cell Size的Width和Height改成与自定义的Cell一样的95*116,图8
4、选中CollectionView的属性,可以修改其属性,比如是垂直滑动,还是水平滑动,选择Vertical或Horizontal
5、在ViewDidLoad方法中声明Cell的类,在ViewDidLoad方法中添加,此句不声明,将无法加载,程序崩溃
1
// KCellID为宏定义 @"CollectionCell"
2
[self.collection registerClass:[CollectionCell
class
] forCellWithReuseIdentifier:KCellID];
1
@interface ViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
01
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
02
{
03
// 每个Section的item个数
04
return
12;
05
}
06
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
07
{
08
09
CollectionCell *cell = (CollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:KCellID forIndexPath:indexPath];
10
11
// 图片的名称
12
NSString *imageToLoad = [NSString stringWithFormat:@
"%d.JPG"
, indexPath.row];
13
14
// 设置label文字
15
cell.label.text = [NSString stringWithFormat:@
"{%ld,%ld}"
,(
long
)indexPath.row,(
long
)indexPath.section];
16
17
// 设置imageView的图片
18
cell.imageView.image = [UIImage imageNamed:imageToLoad];
19
20
return
cell;
21
22
}
点击某项后跳转事件与UITableView类似,实现代理方法
1
-(
void
)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
- iOS CollectionView
- iOS---collectionView
- ios collectionView
- iOS collectionView使用
- iOS collectionview小结
- iOS开发中的collectionView
- iOS CollectionView简单使用
- collectionView的使用iOS
- iOS collectionView的使用
- iOS-CollectionView 基础
- iOS-CollectionView流水布局
- IOS CollectionView 线性布局
- iOS--集合视图CollectionView
- iOS 单元格CollectionView简介
- IOS开发之CollectionView
- iOS collectionView返回顶部
- iOS collectionView相册滑动
- iOS collectionView 单行刷新
- Android中使用SQLite数据库详解
- Android 学习之自定义View
- 受到助教的启发,来写博文记录自己在学习上的成长
- 线性时间排序: 三种非基于比较的内部排序算法
- AngularJS控制器之间的通信
- iOS---collectionView
- python 多线程编程
- ctrl+alt+方向键 与win7的旋转屏幕冲突解决
- ZuoyeOc--父打子
- Android 4.3 BLE 开发samples for SDK单独下载
- IO Brief
- Qt常用快捷键
- cocos2d-x 学习笔记
- Spark中的combineByKey