【发布时间】:2014-12-27 10:46:58
【问题描述】:
我正在尝试为 iOS 应用程序创建一个简单的图库。我正在使用 ViewController 内的 UICollectionView。一切似乎都工作正常,但我无法获得我试图用来在集合视图单元格内显示的图像。我将在这里转储一些代码,我一直在拼命寻找解决方案,但似乎没有任何效果。
这是 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 出口连接到集合视图单元内的图像视图):
#import <UIKit/UIKit.h>
@interface customCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
关于故事板中的连接,一切都应该没问题,在这里也很难展示,但无论如何我都会尝试解释。 UICollectionView 在 gallery.h 文件中的连接方式如下:
@interface gallery : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
@end
情节提要中的集合视图单元格具有自定义类“customCell”和标识符“customCell”。
如果有人能指出我的错误可能在哪里,那就太棒了! 谢谢!
【问题讨论】:
-
在cellForItem方法中添加断点或日志语句,并检查图像视图属性的值。
标签: ios objective-c uiimageview uicollectionview