UICollectonView

- (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;

}

时间: 2024-08-06 08:14:00

UICollectonView的相关文章

比 UICollectionView更好用的IGListKit教程

原文:IGListKit Tutorial: Better UICollectionViews 作者:Ryan Nystrom 译者:kmyhy 每个 app 都以同样的方式开始:几个界面,几颗按钮,一两个 list.但随着进度的进行以及 app 膨胀,功能开始发生变化.你简单的数据源开始在工期和产品经理的压力下变得支离破碎.再过一久,你留下一堆庞大得难以维护的 view controller.今天,IGListKit 来拯救你了! IGListKit 专门用于解决在使用 UICollectio

(原)UICollectionView的基本使用

UICollectionView基本使用 学习iOS一段时间了,早听说了UICollectionView的强大一直没有机会使用,今天自己研究了一下. UICollectonView类似UITableView 用法基本相似, UICollectionView 更灵活,更强大UICollectonView 将其子视图的位置,大小和外观的控制权委托给一个单独的布局对象.通过提供一个自定义布局对象,你几乎可以实现任何你能想象到得布局. 下面是UICollectionView 常用的代理方法. //定义展