我正在尝试为 iOS 应用程序创建一个简单的图库。我正在使用 ViewController 内的 UICollectionView。一切似乎都工作正常,但我无法获得我试图用来在 Collection View 单元格内显示的图像。我将在这里转储一些代码,我一直渴望找到解决方案,但似乎没有任何效果。
这是gallery.m 文件:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
{
//just a random value for testing
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
//another random test value
return 5;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"customCell";
customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier
forIndexPath:indexPath];
//UIImage *image = [dataArray objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"skull.png"];
//I tried all of the below code to get the image to show but nothing seemed to work
//[cell.imageView setImage:[UIImage imageNamed:@"skull.png"]];
//[cell.imageView setNeedsDisplay];
//[cell.imageView reloadInputViews];
return cell;
}
这是 customCell.h 文件(UIImageView socket 连接到 Collection View 单元格内的 ImageView ):
#import <UIKit/UIKit.h>
@interface customCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
关于 Storyboard中的连接,一切都应该没问题,在这里也很难展示,但无论如何我都会尝试解释。
UICollectionView 在 gallery.h 文件中连接,如下所示:
@interface gallery : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end
Storyboard中的 Collection View 单元格具有自定义类“customCell”和标识符“customCell”。
如果有人能指出我的错误可能在哪里,那就太棒了!
谢谢!
最佳答案
我有这个非常相似的问题。使用 Storyboard 时,我无法在 uicollection View 单元格中显示 ImageView 。我刚刚在 CollectionViewController 的 ViewDidLoad 中删除了以下代码:
//[self.collectionView registerClass:[CustomCollectionViewCell class]forCellWithReuseIdentifier:reuseIdentifier];
现在 ImageView 显示在单元格内。
关于ios - Collection View 单元格不在 ImageView 中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27666495/