UICollectionView在其他控制器中的使用.

0.遵守代理
self.collectionView.delegate = self;self.collectionView.dataSource = self;

1.注冊(cè) cell 類
//注冊(cè) collectionViewCell [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseID];

2.創(chuàng)建 collectionView

  • (void)setupCollectionView{ UICollectionViewFlowLayout *layout = [self layout]; UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 104, SCREENW, SCREENH - 104) collectionViewLayout:layout]; self.collectionView = collectionView;// collectionView.scrollEnabled = NO; collectionView.pagingEnabled = YES;// collectionView.showsHorizontalScrollIndicator = NO; collectionView.backgroundColor = [UIColor lightGrayColor]; collectionView.bounces = NO; [self.view addSubview:collectionView];}

2.1注意:代碼創(chuàng)建itemCell 必須指定 layout

  • (UICollectionViewFlowLayout *)layout{ UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 0; layout.itemSize = CGSizeMake(100, 200); layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; return layout;}

3.實(shí)現(xiàn)代理方法

  • (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1;}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 4;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath]; NSLog(@"%@",NSStringFromCGRect(cell.frame)); cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0]; return cell;}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容