码迷,mamicode.com
首页 > 其他好文 > 详细

UICollectonView

时间:2015-11-12 16:22:30      阅读:316      评论:0      收藏:0      [点我收藏+]

标签:

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

}


UICollectonView

标签:

原文地址:http://my.oschina.net/u/2450398/blog/529382

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!