iOS UICollectionView 純程式碼,無xib

來源:互聯網
上載者:User

iOS UICollectionView 純程式碼,無xib
//

1) 必須使用下面的方法進行Cell類的註冊:

// - (void)registerClass:forCellWithReuseIdentifier:

// - (void)registerClass:forSupplementaryViewOfKind:withReuseIdentifier:

// - (void)registerNib:forCellWithReuseIdentifier:

// - (void)registerNib:forSupplementaryViewOfKind:withReuseIdentifier:


- (void)viewDidLoad

{

//初始化

UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayoutalloc] init];

[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

self.collectionView = [[UICollectionViewalloc]initWithFrame:CGRectMake(0, kNavHeight, kDeviceWidth,kDeviceHeight-kNavHeight*2-kTabBarHeight-20)collectionViewLayout:flowLayout];

//註冊

[self.collectionView registerClass:[VideoCell class]forCellWithReuseIdentifier:@"cell"];

//設定代理

self.collectionView.delegate = self;

self.collectionView.dataSource = self;

[self.view addSubview:self.collectionView];

}

#pragma mark - collectionView delegate

//設定分區

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView*)collectionView

{

return 1;

}


//每個分區上的元素個數

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

return 24;

}


//設定元素內容

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

{

static NSString *identify = @"cell";

VideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];

[cell sizeToFit];

if (!cell) {

}

VideoModel *model = [self.videoModels objectAtIndex:indexPath.row];

NSURL *url = [NSURL URLWithString:model.videoImgURL];

[cell.imgView setImageWithURL:url];

cell.titleLbale.text = model.videoTitle;

return cell;

}


//設定元素的的大小框

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

UIEdgeInsets top = {5,10,15,5};

return top;

}


//設定頂部的大小

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section

{

CGSize size={0,0};

return size;

}

//設定元素大小

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

{

return CGSizeMake(240,(kDeviceHeight-kNavHeight*2-kTabBarHeight-20)/4.0);

}


//點擊元素觸發事件

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"%@",indexPath);

DetailVideoViewController *detailVC = [[DetailVideoViewControlleralloc]init];

[self.navigationController pushViewController:detailVCanimated:YES];

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.