[英]cell of collection view not displaying in row
我有12張圖片,我想在UICollectionView
顯示此圖片, UICollectionView
包含4行,每行包含3張圖片。
這是我的.h文件代碼
@interface MyMusic : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *_collectionView;
}
@end
這是我的.m文件代碼
- (void)viewDidLoad{
enter code hereUICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(100, 100)];
[flowLayout setMinimumLineSpacing:5];
[flowLayout setMinimumInteritemSpacing:5];
[flowLayout setSectionInset:UIEdgeInsetsMake(0, 0, 0, 0)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10,238, 300, 200) collectionViewLayout:flowLayout];
_collectionView.backgroundColor = [UIColor redColor];
[_collectionView setDataSource:self];
[_collectionView setDelegate:self];
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
[self.view addSubview:_collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 4;}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{return 3;}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
enter UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
cell.backgroundColor=[UIColor greenColor];
return cell;
}
上面的代碼工作正常,但我的輸出將無法正常顯示
它看起來像這樣
任何人都可以幫助我如何解決這個問題?
您的UICollectionView
寬度為300 UICollectionView
,項目的寬度為100 UICollectionView
。 中間空間是5點,所以
100*3 + 5*2 = 310.
這就是為什么您僅看到2列的原因。
使您的單元格的寬度稍小一些,例如290
或將中間間距設置為0。
主要規則-您的UICollecitionView
寬度應大於或等於單元格widths + interitem spacing
總和widths + interitem spacing
更換
[flowLayout setItemSize:CGSizeMake(100, 100)];
通過
[flowLayout setItemSize:CGSizeMake(75,100 )];
我僅將75用於試用目的,並考慮使用iPhone屏幕。因此,列寬相等。
但是,您不應使用硬編碼的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.