我以编程方式向控制器添加了UICollectionView,但无论如何设置,在调用dequeueReusableCellWithReuseIdentifier时无法获得有效的UICollectionViewCell。显示代码,一些信息可能会有所帮助之前:派生的UICollectionViewCell总是从dequeueReusableCellWithReuseIdentifier返回空值
- 我使用的是最新的XCode
- 我我不使用故事板 iPad上中定位iOS 8.1
- 我有一个导航控制器设置,我还使用核心数据
下面的代码:
或者Controller.h
@interface ViewController : UIViewController<UIImagePickerControllerDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
@end
Controller.m或者
@interface ViewController()
@property (strong, nonatomic) NSMutableArray *images;
@property (strong, nonatomic) UICollectionView *collectionView;
@end
// called by a button in the navigation bar
- (void)addMedia:(id)sender {
// load test images
if ([self.images count] == 0) {
NSBundle *myBundle = [NSBundle mainBundle];
NSArray *imagesPaths = [myBundle pathsForResourcesOfType:@"png" inDirectory:nil];
for (NSString *path in imagesPaths) {
UIImage *newImage = [[UIImage alloc] initWithContentsOfFile:path];
[self.images addObject:newImage];
}
self.introLabel.hidden = YES;
CGRect rect = CGRectMake(self.view.frame.size.width/2 - 250,
self.view.frame.size.height/2 - 250, 500, 500);
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(120, 120);
layout.minimumInteritemSpacing = 10;
self.collectionView = [[UICollectionView alloc] initWithFrame:rect collectionViewLayout:layout];
UINib *cellNib = [UINib nibWithNibName:@"ImageViewCell" bundle:nil];
[self. collectionView registerNib:cellNib forCellWithReuseIdentifier:@"imageCell"];
//[self.collectionView registerClass:[ImageViewCell class] forCellWithReuseIdentifier:@"imageCell"];
[self.collectionView setDelegate:self];
[self.collectionView setDataSource:self];
[self.view addSubview:self.collectionView];
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [self.images count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// THIS IS ALWAYS NULL
ImageViewCell *cell = (ImageViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"imageCell" forIndexPath:indexPath];
cell.image.image = [self.images objectAtIndex:indexPath.row];
return cell;
}
ImageViewCell.h
@interface ImageViewCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *image;
@end
ImageViewCell.m
@implementation ImageViewCell
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
// THIS IS ALWAYS NULL!
return self;
}
@end
在ImageViewCell.xib我有一个UICollectionCellView作为马在视图中,其中包含我试图填充的UIImageView; UICollectionCellView指向ImageViewCell(派生类)。
我应该提到,我尝试了与一个单一控制器的新项目相同的方法,并以编程方式添加UICollectionView,它的工作原理,所以我必须缺少一些愚蠢或做一些稍微不同的东西,但我不知道它。
[编辑]:用解决方案更改了代码。
我不知道这是否是你的问题,但如果你在xib中创建单元格,你应该注册nib而不是类。你的应用崩溃了吗?如果是这样,你会得到什么错误? – rdelmar 2014-11-24 00:49:16
'dequeueReusable ...'只在你注册它的笔尖时自动创建一个单元格。 – 2014-11-24 01:21:16