UICollectView小例子

来源:互联网 发布:淘宝男装店铺排行榜 编辑:程序博客网 时间:2024/06/05 18:36

实现的效果图如下:


这个collectview有四个section,分别有4,1,4,4个item。

- (void)viewDidLoad

{

    [superviewDidLoad];

self.collectView.delegate =self;

    self.collectView.dataSource =self;


self.identifys =@[@"cell1",@"cell2",@"cell3",@"cell4"];

    NSArray *classes =@[[oneCellclass],[twoCellclass],[threeCellclass],[FourCellclass]];

   for (int i =0; i <  4; i++) {

        [selfregisterForCellFromClass:classes[i] indentify:self.identifys[i]];

    }

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayoutalloc]init];

#warning 设定全局行间距

    flowLayout.minimumLineSpacing =20;

    flowLayout.headerReferenceSize =CGSizeMake(0,20);

    flowLayout.sectionInset =UIEdgeInsetsMake(0,10, 0, 10);

#warning 下面一句是必须要写的

    self.collectView.collectionViewLayout = flowLayout;

}


//这样写是提取出注册函数

- (void)registerForCellFromClass:(Class)class indentify:(NSString *)indentify

{

    [self.collectViewregisterClass:class forCellWithReuseIdentifier:indentify];

}



- (UICollectionViewCell *)returnCellFromIndexPath:(NSIndexPath *)indexPath

{

    return [self.collectViewdequeueReusableCellWithReuseIdentifier:self.identifys[indexPath.section]forIndexPath:indexPath];

}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    UICollectionViewCell *cell;

   switch (indexPath.section) {

       case kFirst:{

         

           oneCell *cell1 = (oneCell *)[selfreturnCellFromIndexPath:indexPath];

            cell1.imageView.image = [UIImageimageNamed:@"20111021190309-565119129.jpg"];

            cell1.label.text =@"girl";

            cell = cell1;

        }

           break;

            

       case kSecond: {

            

           twoCell *cell2 = (twoCell *)[selfreturnCellFromIndexPath:indexPath];

            cell2.imageView.image = [UIImageimageNamed:@"20111021190309-565119129.jpg"];

            cell2.titleLabel.text =@"hello";

            cell2.detailLabel.text =@"hi";

            cell = cell2;

        }

           break;

       case kThird: {

            

           threeCell *cell3 = (threeCell *)[selfreturnCellFromIndexPath:indexPath];

            cell3.label.text =@"nihao";

            cell = cell3;

        }

           break;

       case kFour:{

            

           FourCell *cell4 = (FourCell *)[selfreturnCellFromIndexPath:indexPath];

            cell4.imageView.image = [UIImageimageNamed:@"20111021190309-565119129.jpg"];

            cell = cell4;

        }

           break;

       default:

           break;

    }

    cell.backgroundColor = [UIColorwhiteColor];

   return cell;

}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

   CGSize size;

   switch (indexPath.section) {

       case kFirst:

            size =CGSizeMake(140,45);

           break;

       case kSecond:

            size =CGSizeMake(300,80);

           break;

       case kThird:

            size =CGSizeMake(140,40);

           break;

           case kFour:

            size =CGSizeMake(60,60);

           break;

       default:

           break;

    }

   return size;

}



0 0
原创粉丝点击