UICollectionView的使用方法
来源:互联网 发布:广元外卖软件 编辑:程序博客网 时间:2024/06/08 14:25
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorlightGrayColor];
//确定是水平滚动,还是垂直滚动
UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayoutalloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
_collectionView=[[UICollectionViewalloc] initWithFrame:CGRectMake(0,64, self.view.frame.size.width,self.view.frame.size.height-64)collectionViewLayout:flowLayout];
_collectionView.dataSource=self;
_collectionView.delegate=self;
[_collectionViewsetBackgroundColor:[UIColorgreenColor]];
//注册Cell,必须要有
[_collectionViewregisterClass:[UICollectionViewCellclass] forCellWithReuseIdentifier:@"UICollectionViewCell"];
[self.viewaddSubview:_collectionView];
}
#pragma mark - collectionView dataSource Or delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return50;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
staticNSString * CellIdentifier = @"UICollectionViewCell";
UICollectionViewCell * cell = [collectionViewdequeueReusableCellWithReuseIdentifier:CellIdentifierforIndexPath:indexPath];
cell.backgroundColor = [UIColorcolorWithRed:((10 * indexPath.row) /255.0) green:((20 * indexPath.row)/255.0)blue:((30 * indexPath.row)/255.0)alpha:1.0f];
UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 20,20)];
label.textColor = [UIColorredColor];
label.text = [NSStringstringWithFormat:@"%ld",(long)indexPath.row];
for (id subViewin cell.contentView.subviews) {
[subView removeFromSuperview];
}
[cell.contentViewaddSubview:label];
return cell;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return1;
}
//定义每个UICollectionView的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
returnUIEdgeInsetsMake(5,5, 5,5);
}
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionViewcellForItemAtIndexPath:indexPath];
//临时改变个颜色,看好,只是临时改变的。如果要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~)
cell.backgroundColor = [UIColorgreenColor];
NSLog(@"item======%ld",(long)indexPath.item);
NSLog(@"row=======%ld",(long)indexPath.row);
NSLog(@"section===%ld",(long)indexPath.section);
}
//定义每个Item的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
returnCGSizeMake(100,100);
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
returnYES;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- UICollectionView的使用方法
- UICollectionView的使用方法
- UICollectionView的使用方法
- UICollectionView的使用方法及demo
- UICollectionView的使用方法及demo
- UICollectionView使用方法
- UICollectionView使用方法
- iOS开发中UICollectionView的使用方法
- UICollectionView基本使用方法
- iOS UICollectionView基础使用方法
- IOS中集合视图UICollectionView中DecorationView的简易使用方法
- IOS中集合视图UICollectionView中Decoration View的简易使用方法。
- IOS6- UICollectionView的使用
- ios UICollectionView 的用法
- ios UICollectionView的使用
- UICollectionView的HeaderView、FooterView
- UICollectionView的简单使用
- UICollectionView 的使用
- 使用inline-block实现图片列表展示(对比float的实现)
- 调用函数排序
- WebService 之 WSDL文件 讲解
- 子类、父类、静态成员变量,构造函数的执行顺序
- Weka算法介绍[
- UICollectionView的使用方法
- 【Mysql】利用group by附带having进行聚类查询
- Markdown 11种基本语法
- 详解用CSS绘制3D旋转立方体
- 杭电2111(Saving HDU)
- oracle创建用户并指定表空间
- 双调排序:从串行到并行,以及OpenCL上的实现
- Xiph opus音频编码器试用
- Postman 发送json格式请求