你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS UICollectionView 純代碼,無xib

iOS UICollectionView 純代碼,無xib

編輯:IOS開發綜合
//

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

}

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved