[IOS UICollectionView模版]

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   檔案   

建立CollectionCell模版:

1、建立類CollectionCell繼承自UICollectionViewCell

2、建立Xib,命名為CollectionCell.xib

a.選中CollectionCell.xib刪掉預設的View,從控制項中拖一個Collection View Cell(圖3)到畫布中,設定大小為95*116;

 

b.選中剛剛添加的Cell,更改類名為CollectionCell,4

c.在CollectionCell.xib的CollectionCell中添加一個ImageView和一個Label(圖5)

d.建立映射, 圖6,圖7

e.選中CollectionCell.m , 重寫init方法 

- (id)initWithFrame:(CGRect)frame  {      self = [super initWithFrame:frame];      if (self)      {          // 初始化時載入collectionCell.xib檔案          NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"CollectionCell" owner:self options:nil];                    // 如果路徑不存在,return nil          if (arrayOfViews.count < 1)          {              return nil;          }          // 如果xib中view不屬於UICollectionViewCell類,return nil          if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[UICollectionViewCell class]])          {              return nil;          }          // 載入nib          self = [arrayOfViews objectAtIndex:0];      }      return self;  }  
 

f.選中CollectionCell.xib 修改其identifier為CollectionCell。

 

======================================================

indexviewcontroller.h

#import <UIKit/UIKit.h>#import "CollectionCell.h"@interface indexviewcontroller : UIViewController <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>@end

indexviewcontroller.m

#import "indexviewcontroller.h"@interface indexviewcontroller ()@end@implementation indexviewcontrollerUICollectionView *cv;- (void)viewDidLoad{    [super viewDidLoad];    [self loadcollectionview];}-(void)loadcollectionview{    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];    [flowLayout setItemSize:CGSizeMake(80, 100)];//設定cell的尺寸    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];//設定其布局方向        cv=[[UICollectionView alloc]initWithFrame:CGRectMake(40,100,240,400) collectionViewLayout:flowLayout];        [cv registerClass:[CollectionCell class] forCellWithReuseIdentifier:@"CollectionCell"];        [cv setBackgroundColor:[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.75]];    cv.delegate=self;    cv.dataSource=self;    [self.view addSubview:cv];}#pragma mark -- UICollectionViewDataSource//定義展示的UICollectionViewCell的個數-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{    return 6;}//定義展示的Section的個數-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{    return 1;}//每個UICollectionView展示的內容-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{    static NSString * CellIdentifier = @"CollectionCell";    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];        cell.backgroundColor=[UIColor grayColor];        return cell;}#pragma mark --UICollectionViewDelegateFlowLayout//定義每個UICollectionView 的大小- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{    return CGSizeMake(80, 100);}//定義每個UICollectionView 的 margin-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{    return UIEdgeInsetsMake(5, 5, 5, 5);}#pragma mark --UICollectionViewDelegate//UICollectionView被選中時調用的方法-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{    UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];    cell.backgroundColor = [UIColor whiteColor];}//返回這個UICollectionView是否可以被選擇-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{    return YES;}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

[IOS 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.