gpt4 book ai didi

ios - 如何创建自定义 UICollectionViewCell 并将来自 URL 的图像添加到它?

转载 作者:可可西里 更新时间:2023-11-01 06:18:04 29 4
gpt4 key购买 nike

我想把我从 flickr 上得到的图片加载到 uicollectionsview 中我遵循了很多指南,但当我必须这样做时,它们都崩溃了

    cell.imageView.image

它基本上说它无法在 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;
}

recipieImageView 是我得到的一些教程。我的单元格在 Storyboard中名为 flickerCell,photoURLs 有 33 个我可以在代码中查看的对象。所以一切都在那里。 photoURLs 也是一个 NSMutablearray然而。在这种方法中,我的 recipeImageView 返回 nill ???我有一个带有 imageView 的 cell.h

#import <UIKit/UIKit.h>

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

@end

所以如果有人知道我可以显示 photoURLs 在 uicollectionview 中的所有图像的方法,那将非常有帮助

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

- (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;
}

再次更新

- (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;

}有了这个 viewdidload

    [self.collectionView registerClass:[Cell class] forCellWithReuseIdentifier:@"flickerCell"];

flickerCell 的 NSLOG 返回这个:

    flickercell = <Cell: 0x9dec2f0; baseClass = UICollectionViewCell; frame = (0 0; 120 115); layer = <CALayer: 0x9dec430>>

最佳答案

据我所知,您正在使用自定义 UICollectionViewCell。但是,在您粘贴的方法中,您创建了一个指向没有内置 UIImageView 的 UICollectionViewCell 的指针。所以这就是为什么你在第一次尝试时会出错。关于第二个 - 您是否向 CollectionView 注册了您的自定义 UICollectionViewCell 类?

======

编辑:在与发帖人长时间交谈后,我们解决了所有问题。

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

关于ios - 如何创建自定义 UICollectionViewCell 并将来自 URL 的图像添加到它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20451578/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com