【问题标题】:IOS Collection View load Image from custom cell classIOS Collection View load Image from custom cell class
【发布时间】:2016-07-28 04:44:07
【问题描述】:

我正在尝试使用自定义 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 类加载不同的图像并显示在每个单元格上。

【问题讨论】:

标签: ios objective-c uicollectionview uicollectionviewcell


【解决方案1】:

尝试在你的customCell MyCell 中实现UICollectionViewCellprepareForReuse 方法

-(void)prepareForReuse {

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

尝试awakeFromNib 并删除prepareForReuse

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

【讨论】:

  • 还是没有变化,图片没有加载。
  • 是的,它正在显示来自 collectionView 自定义类的图像。
  • 我设置了一个断点prepareForReuse(),它没有被调用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-17
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多