如何创建自定义UICollectionViewCell并从URL添加图像?

Ken*_*yVB 3 objective-c uiimage ios uicollectionview uicollectionviewcell

我想把我从flickr获得的图像加载到uicollectionsview我跟随分配指南但是当我必须这样做时它们都会分崩离析

    cell.imageView.image
Run Code Online (Sandbox Code Playgroud)

它基本上说它无法在uicollectionsviewcell的对象中找到imageView

我也试过这样的方法,但它没有用

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";

UICollectionViewCell *flickerCell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
flickerCell.backgroundColor = [UIColor whiteColor];
UIImageView *recipeImageView = (UIImageView *)[flickerCell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
[flickerCell addSubview:recipeImageView];
return flickerCell;
}
Run Code Online (Sandbox Code Playgroud)

recipieImageView是我得到一些教程的东西.我的单元格在故事板中被命名为flickerCell,而且photURLs有33个我可以在代码中查看的对象.所以它就在那里.还有photoURLs是一个NSMutablearray.在这个方法中我的recipeImageView返回nill ??? 我有一个cell.h,它有imageView

#import <UIKit/UIKit.h>

@interface Cell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;

@end
Run Code Online (Sandbox Code Playgroud)

所以,如果有人知道一种方式,我可以在uicollectionview中显示photoURLs所有的图像,这将是非常有帮助的

更新 我现在尝试了一种新方法,但仍然无法正常工作.imageView返回nil

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";

Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

flickerCell.backgroundColor = [UIColor whiteColor];
imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
return flickerCell;
}
Run Code Online (Sandbox Code Playgroud)

再次更新

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"flickerCell";
Cell *flickerCell = (Cell*) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSLog(@"flickercell = %@", flickerCell);
flickerCell.backgroundColor = [UIColor whiteColor];
flickerCell.imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];
return flickerCell;
Run Code Online (Sandbox Code Playgroud)

在viewdidload中使用它

    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"flickerCell"];
Run Code Online (Sandbox Code Playgroud)

flickerCell的NSLOG返回:

    flickercell = <Cell: 0x9dec2f0; baseClass = UICollectionViewCell; frame = (0 0; 120 115); layer = <CALayer: 0x9dec430>>
Run Code Online (Sandbox Code Playgroud)

AXE*_*AXE 18

我得到它你正在使用自定义UICollectionViewCell.但是在您粘贴的方法中,您创建了一个指向UICollectionViewCell的指针,该指针没有内置的UIImageView.这就是为什么你在第一次尝试时得到错误的原因.关于第二个 - 您是否使用CollectionView注册了自定义UICollectionViewCell类?

======

编辑:经过与海报的长时间聊天,我们解决了所有问题.

  • 第一个是注册的类是UICollectionViewCell,而不是继承的自定义类.
  • 第二个问题是视图实际上是在xib文件中,所以registerNib:forCellWithReuseIdentifier:应该使用而不是 registerClass:forCellWithReuseIdentifier:
  • 第三个是自定义单元格是在storyboard xib中配置而不是自己的xib文件,它与自定义类连接(在我们的例子中为"Cell")
  • 第四个主要问题是UIImage被错误地创建,imageNamed:只能用于嵌入项目中的图像.在我们的例子中创建UIImage的正确方法是:flickerCell.imageView.image = [UIImage imageNamed:[photoURLs objectAtIndex:indexPath.row]];