简体   繁体   English

集合视图的单元格未显示在行中

[英]cell of collection view not displaying in row

I have 12 images and i want to display this images in UICollectionView with 4 rows where each rows have 3 images. 我有12张图片,我想在UICollectionView显示此图片, UICollectionView包含4行,每行包含3张图片。

Here is my .h file code 这是我的.h文件代码

    @interface MyMusic : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>
{
UICollectionView *_collectionView;
}
@end

and here is my .m file code 这是我的.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;
}

above code working fine but my output will not come in proper 上面的代码工作正常,但我的输出将无法正常显示

its looking like this 它看起来像这样

在此处输入图片说明

any one can help me how can i solve this ? 任何人都可以帮助我如何解决这个问题?

Your UICollectionView width is 300 points , and your item's width is 100 points . 您的UICollectionView宽度为300 UICollectionView ,项目的宽度为100 UICollectionView Interitem space is 5 points, so 中间空间是5点,所以

100*3 + 5*2 = 310. 

That's why you see only 2 columns. 这就是为什么您仅看到2列的原因。

Make your cells' width a little smaller, for example 290 使您的单元格的宽度稍小一些,例如290

Or set interitem spacing to 0. 或将中间间距设置为0。

Main rule - your UICollecitionView width should be larger or equal to sum of your cells widths + interitem spacing 主要规则-您的UICollecitionView宽度应大于或等于单元格widths + interitem spacing总和widths + interitem spacing

replace 更换

[flowLayout setItemSize:CGSizeMake(100, 100)];

by 通过

[flowLayout setItemSize:CGSizeMake(75,100 )]; 

I am using 75 just for the trial purpose and consider iphone screen.so that width of column are equal. 我仅将75用于试用目的,并考虑使用iPhone屏幕。因此,列宽相等。

However you should not use hard-coded values. 但是,您不应使用硬编码的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM