IOS Collection 查看来自自定义单元格的加载图像 class

IOS Collection View load Image from custom cell class

我正在尝试使用自定义 collectionViewCell class 来实现 collectionView。下面的代码工作正常,图像是从 Collection 视图 class 本身加载的。

customCollectionView.m

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";        
    MyCell *cell =  (MyCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];        
    UIImage *Img = [UIImage imageNamed:[Images objectAtIndex:indexPath.row]];
    cell.myImageView.image = Img;
    cell.layer.borderWidth=2.0f;
    cell.layer.borderColor=[UIColor lightGrayColor].CGColor;
    return cell;
}

MyCell.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

而当我将图像加载部分移至自定义 CollectionViewCell class MyCellimage 未显示。

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
         UIImage *Img = [UIImage imageNamed:@"AppIcon.png"];
         _myImageView.image = Img;

    }
    return self;
}

我的目的是在特定时间间隔从 MyCell class 加载不同的图像并显示在每个单元格上。

尝试在您的 customCell MyCell

中实现 UICollectionViewCellprepareForReuse 方法
-(void)prepareForReuse {

    _myImageView.image = [UIImage imageNamed:@"AppIcon.png"];
}

尝试 awakeFromNib 并移除 prepareForReuse

- (void)awakeFromNib {
    _myImageView.image = [UIImage imageNamed:@"AppIcon.png"];
}