我正在使用 UICollectionView 显示图像,我已经存储在这样的数组中。
newsPhotos = [NSArray arrayWithObjects"photo1.png", @"photo2.png", @"photo3.png", @"photo4.png", @"photo5.png", @"photo6.png", @"photo7.png", @"photo8.png", @"photo9.png", @"photo10.png", Nil];
我的想法是在这个订单上显示网格
1 | 2
3 | 4
5 | 6
7 | 8
9 | 10
所以两张照片,我的 UICollectionView 方法如下所示:
- (NSInteger)collectionViewUICollectionView *)view numberOfItemsInSectionNSInteger)section {
return 2;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return newsPhotos.count;
}
- (UICollectionViewCell *)collectionViewUICollectionView *)cv cellForItemAtIndexPathNSIndexPath *)indexPath {
Cell *cell = [cv dequeueReusableCellWithReuseIdentifier"Cell" forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.section]];
cell.textLabel.text = @"Data";
return cell;
}
但它的打印是这样的
1 | 1
2 | 2
3 | 3
etc..
我明白它为什么会发生,只是不知道如何解决它。有什么帮助吗?
谢谢
试试这个。
-(NSInteger)numberOfSectionsInCollectionView:
(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionViewUICollectionView *)collectionView
numberOfItemsInSectionNSInteger)section
{
return newsPhotos.count;
}
-(UICollectionViewCell *)collectionViewUICollectionView *)collectionView
cellForItemAtIndexPathNSIndexPath *)indexPath
{
// Try and use custom cell by placing a custom cell on your collection view,assign a new class to that cell and then configure that cell as
customCellClass *myCell = [collectionView dequeueReusableCellWithReuseIdentifier"MyCell" forIndexPath:indexPath];
// also place an image view on your custom cell, make an outlet of that image view in your custom cell class and use:
myCell.imageview.image = [UIImage imageNamed:[newsPhotos objectAtIndex:indexPath.row]];
return myCell;
}
现在要每行显示两个图像,您可以相应地调整单元格和 ImageView 的大小,使其每行仅显示两个图像。 希望对你有帮助
关于ios - 在 UICollectionView 中每个部分显示两个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22651119/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |