IOS Collection从自定义单元类中查看加载图像

时间:2016-07-28 04:44:08

标签: ios objective-c uicollectionview uicollectionviewcell

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

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

当我将图片加载部件移至自定义CollectionViewCellMyCell时,image未显示。

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

    }
    return self;
}

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

1 个答案:

答案 0 :(得分:1)

尝试在customCell prepareForReuse

中实施UICollectionViewCell MyCell方法
-(void)prepareForReuse {

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

尝试awakeFromNib并移除prepareForReuse

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