标签:
所属controller要遵循三个协议:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
[flowLayout setItemSize:CGSizeMake(70, 100)];//设置cell的尺寸
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置其布局方向
flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);//设置其边界
//其布局很有意思,当你的cell设置大小后,一行多少个cell,由cell的宽度决定
_collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 320,self.view.frame.size.height) collectionViewLayout:flowLayout];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerClass:[BMCollectionCell class] forCellWithReuseIdentifier:CELL_ID];
[self.view addSubview:_collectionView];
[_collectionView release];
//collectionView的代理方法
#pragma mark - collectionView dataSource Or delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 32;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BMCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDforIndexPath:indexPath];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath
{
NSLog(@"%s",__FUNCTION__);
}
标签:
原文地址:http://www.cnblogs.com/shengjianzhou/p/4483386.html