iOS 建立UICollectionView的例子

來源:互聯網
上載者:User

1.建立自訂UICollectionViewCell

選中工程,右鍵-New File…選擇“Cocoa Touch Class”-Next,選擇繼承於UICollectionViewCell類,給個合理的名稱CollectionViewCell,再Next完成。

1建立

在UICollectionViewCell中定義你所需要的控制項

//圖片
@property(strong,nonatomic)UIImageView *fruitImage;
//標題
@property(strong,nonatomic)UILabel *fruitTitle;
//價格
@property(strong,nonatomic)UILabel *priceLabel;

在UICollectionViewCell.m檔案中重寫


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _fruitImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth/2-1 , kScreenHeight/3.4)];
        _fruitImage.image = [UIImage imageNamed:@"pic_fruit"];
        [self.contentView addSubview:_fruitImage];
        
        _fruitTitle = [[UILabel alloc]initWithFrame:CGRectMake(7, kScreenHeight/3.4 , kScreenWidth/2-6, 50)];
        _fruitTitle.numberOfLines = 0;
        _fruitTitle.font = [UIFont systemFontOfSize:13];
        _fruitTitle.textColor = [UIColor grayColor];
        [self.contentView addSubview:_fruitTitle]; 
    }
     return self;
}

2.在ViewController.m檔案中建立UICollectionView執行個體

//建立collectionView
-(void)creatCollectionView{
    //1.初始化layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    //設定collectionView滾動方向
    //    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    //設定headerView的尺寸大小
    //    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 100);
    //該方法也可以設定itemSize
    //    layout.itemSize =CGSizeMake(0, 150);
    
    //2.初始化collectionView
    mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kScreenHeight /1.1, kScreenWidth, kScreenHeight*1.6) collectionViewLayout:layout];
    [self.upDownScrollView addSubview:mainCollectionView];
    mainCollectionView.backgroundColor = [UIColor colorWithRed:235/255.0 green:235/255.0 blue:235/255.0 alpha:1];
    
    //3.註冊collectionViewCell
    //注意,此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致 均為 cellId
    [mainCollectionView registerClass:[FruitCollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //    //註冊headerView  此處的ReuseIdentifier 必須和 cellForItemAtIndexPath 方法中 一致  均為reusableView
    //    [mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.設定代理
    mainCollectionView.delegate = self;
    mainCollectionView.dataSource = self;
 
}

3.實現UICollectionView代理方法

#pragma mark collectionView代理方法
//返回section個數
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
 
//每個section的item個數
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 6;
}
 
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    FruitCollectionViewCell *cell = (FruitCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    
 
    
    if (indexPath.section == 0 && indexPath.row == 1 ) {
        cell.fruitTitle.text = @"正宗台灣產高雄特產熱帶香蕉好吃包郵123";
 
    }else{
        cell.fruitTitle.text = @"火爆進口馬來西亞特產大金龍芒果包郵500g";
    }
    
 
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}
 
//設定每個item的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kScreenWidth/2-4, kScreenHeight/2.5);
 
}
 
//footer的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}
 
//header的size
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
//{
//    return CGSizeMake(10, 10);
//}
 
//設定每個item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 2, 0, 2);
}
 
//設定每個item水平間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 2;
}
 
 
//設定每個item垂直間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5;
}
 
 
//通過設定SupplementaryViewOfKind 來設定頭部或者底部的view,其中 ReuseIdentifier 的值必須和 註冊是填寫的一致,本例都為 “reusableView”
//- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
//{
//    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView" forIndexPath:indexPath];
//    headerView.backgroundColor =[UIColor grayColor];
//    UILabel *label = [[UILabel alloc] initWithFrame:headerView.bounds];
//    label.text = @"這是collectionView的頭部";
//    label.font = [UIFont systemFontOfSize:20];
//    [headerView addSubview:label];
//    return headerView;
//}
 
//點擊item方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FruitCollectionViewCell *cell = (FruitCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSString *msg = cell.fruitTitle.text;
    NSLog(@"%@",msg);
}

看到這裡相信你已經掌握了UICollectionView的使用方法啦

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.