UICollectonView
2015-11-12 00:00
477 查看
- (void)viewDidLoad {
[super viewDidLoad];
[_plantCollectionView registerNib:[UINib nibWithNibName:@"PlantCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
_plantCollectionView.backgroundColor = [UIColor whiteColor];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
//横向
[flowLayout setMinimumInteritemSpacing:7.0f];
//AllLine
[flowLayout setMinimumLineSpacing:0.0f];
[_plantCollectionView setCollectionViewLayout:flowLayout];
arrayPlant = [[NSMutableArray alloc] init];
cHeight = (SCREEN_WIDTH-kdistance*3-kedge*2)*109/80/4;
cWidth = (SCREEN_WIDTH-kdistance*3-kedge*2)/4;
[self getPlantData];
// Do any additional setup after loading the view from its nib.
}
@implementation
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayPlant count];
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PlantCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
// if (cell == nil) {
// NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"PlantCollectionViewCell" owner:self options:nil];
// cell = [array objectAtIndex:0];
// }
cell.backgroundColor = [UIColor whiteColor];
PlantEntity * entity = [arrayPlant objectAtIndex:indexPath.row];
[cell.imageViewPlant sd_setImageWithURL:[NSURL URLWithString:entity.album]];
cell.labelTitle.text = entity.title;
// cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
return cell;
}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(cWidth, cHeight);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
// if (section%4 == 1 ) {
// return UIEdgeInsetsMake(kdistance, kedge , 0, kdistance/2);
// }else if(section%4 == 0){
// return UIEdgeInsetsMake(kdistance, kedge , 0, kdistance/2);
// }
// else{
return UIEdgeInsetsMake(kdistance, kedge , 0, kedge);
// }
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
WebViewController *vc = [[WebViewController alloc] init];
vc.url = @"http://www.baidu.com";
[self.navigationController pushViewController:vc animated:YES];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
[super viewDidLoad];
[_plantCollectionView registerNib:[UINib nibWithNibName:@"PlantCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:CellIdentifier];
_plantCollectionView.backgroundColor = [UIColor whiteColor];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
//横向
[flowLayout setMinimumInteritemSpacing:7.0f];
//AllLine
[flowLayout setMinimumLineSpacing:0.0f];
[_plantCollectionView setCollectionViewLayout:flowLayout];
arrayPlant = [[NSMutableArray alloc] init];
cHeight = (SCREEN_WIDTH-kdistance*3-kedge*2)*109/80/4;
cWidth = (SCREEN_WIDTH-kdistance*3-kedge*2)/4;
[self getPlantData];
// Do any additional setup after loading the view from its nib.
}
@implementation
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayPlant count];
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PlantCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
// if (cell == nil) {
// NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"PlantCollectionViewCell" owner:self options:nil];
// cell = [array objectAtIndex:0];
// }
cell.backgroundColor = [UIColor whiteColor];
PlantEntity * entity = [arrayPlant objectAtIndex:indexPath.row];
[cell.imageViewPlant sd_setImageWithURL:[NSURL URLWithString:entity.album]];
cell.labelTitle.text = entity.title;
// cell.backgroundColor = [UIColor colorWithRed:((10 * indexPath.row) / 255.0) green:((20 * indexPath.row)/255.0) blue:((30 * indexPath.row)/255.0) alpha:1.0f];
return cell;
}
#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(cWidth, cHeight);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
// if (section%4 == 1 ) {
// return UIEdgeInsetsMake(kdistance, kedge , 0, kdistance/2);
// }else if(section%4 == 0){
// return UIEdgeInsetsMake(kdistance, kedge , 0, kdistance/2);
// }
// else{
return UIEdgeInsetsMake(kdistance, kedge , 0, kedge);
// }
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
WebViewController *vc = [[WebViewController alloc] init];
vc.url = @"http://www.baidu.com";
[self.navigationController pushViewController:vc animated:YES];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
相关文章推荐
- [非凡程序员]uitableview uiimage 的手动编写 加密
- UIKit 手写控件 UIImageView 和UITableView
- UICollectionView
- iOS7 UITabBar自定义选中图片显示为默认蓝色的Bug
- IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)
- UI基础知识 -- UITextView UIButton
- UVA 12166 Equilibrium Mobile(贪心,反演)
- 使用Segue在两个UIViewController之间建立联系-NavigationController
- 爬爬爬之路:UI(二) UITextField UIButton 程序生命周期简介
- mysql 连接url中useUnicode=true&characterEncoding=UTF-8 的作用
- 我的iOS学习历程 - UITextField UIButton
- 我的iOS学习历程 - UITextField UIButton
- Quick小白书系列(四)Quick中的Scene
- 学习笔记-七burpsuite的使用
- [TwistedFate]UIButton 应用启动流程
- [TwistedFate]UITextField
- LeetCode Range Sum Query - Immutable
- IOS UIProgressView
- 系统自带的UIBarButtonSystemItem样式
- Spark RDD中Transformation的mapValues、subtract、sample、takeSample详解