UICollectionViewCell 不显示图像

Posted

技术标签:

【中文标题】UICollectionViewCell 不显示图像【英文标题】:UICollectionViewCell does not show images 【发布时间】:2014-01-24 14:14:59 【问题描述】:

如何使用 UICollectionViewCell 获取单元格中显示的图像?

运行以下代码,我在调试器中得到以下输出。 MagazineCell initWithFrame 被调用,当重新加载数据时,setPhoto 被调用。如果我将 setPhoto 代码放在 initWithFrame 方法中,则重新加载将无效。 但模拟器只显示灰色单元格。没有添加图片,没有背景颜色变为黄色/红色。我错过了什么?

[5230:70b] -[WebApi getSurroundImages] [Line 326] do surround composition
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[MagazineCell initWithFrame:] [Line 22] contentView frame: 104.000000
[5230:70b] -[SurroundViewController didComposition] [Line 146] reload Collection View Data
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000
[5230:70b] -[MagazineCell setPhoto:] [Line 38] height: 104.000000, and width 104.000000

我是否要更改以下内容

// in viewDidLoad
//    [self.collectionView registerClass:[MagazineCell class] 

// in - collectionView:cellForItemAtIndexPath:indexPath
[mCell.imageView setImageWithURL:photoURL placeholderImage:[UIImage imageNamed:@"foo.jpg"]];
//        [mCell setPhoto:photoURL];

我在调试器中得到以下输出。显示的图像太小,并且未调用 MagazineCell。不是我想要的

[5402:70b] -[WebApi getSurroundImages] [Line 326] do surround composition
[5402:70b] -[SurroundViewController didComposition] [Line 146] reload Collection View Data

似乎 imageView 无法正常工作、地址错误或其他原因。但我没有看到我的错误...下面是完整的代码:

SurroundViewController.h

#import <UIKit/UIKit.h>
#import "WebApi.h"
#import "SingletonClass.h"

@interface SurroundViewController : UICollectionViewController <WebApiDelegate>

@property (nonatomic, strong) SingletonClass *sshare;
@property (nonatomic, strong) WebApi *swebapi;

@end

SurroundViewController.m

#import "SurroundViewController.h"
#import "MagazineCell.h"
#import "LazyJoe.h"
#import <AFNetworking/UIImageView+AFNetworking.h>

static NSString * const cellID = @"cellID";

@interface SurroundViewController ()

@property (nonatomic, strong) LazyJoe *lazyJoe;

@end

@implementation SurroundViewController

- (void)viewDidLoad

    [super viewDidLoad];
    self.sshare = [SingletonClass sharedInstance];   

    self.lazyJoe = [[LazyJoe alloc]init];
    self.collectionView.collectionViewLayout = self.lazyJoe; // Layout things are in the Flowlayout
    [self.collectionView registerClass:[MagazineCell class] forCellWithReuseIdentifier:cellID]; 

    self.swebapi = [WebApi sharedInstance];
    self.swebapi.delegate = self;    
    [self.swebapi getSurroundImages]; // will call delegate didComposition    


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
    return 1;


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
    return 10;


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

    MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

    mCell.backgroundColor = [UIColor lightGrayColor];

    NSURL *photoURL = [NSURL URLWithString:self.sshare.coData[indexPath.item]];
    //[mCell.imageView setImageWithURL:photoURL placeholderImage:[UIImage imageNamed:@"foo.jpg"]];
    [mCell setPhoto:photoURL];
    
    return mCell;


-(void)didComposition 
    DLog(@"reload Collection View Data");
    [self.collectionView reloadData];

@end

MagazineCell.h

#import <UIKit/UIKit.h>
#import <AFNetworking/UIImageView+AFNetworking.h>

@interface MagazineCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
// is connected to IB with the according CollectionView Cell in the Scene

-(void)setPhoto:(NSURL *)photoURL;

@end

MagazineCell.m

#import "MagazineCell.h"

@implementation MagazineCell

- (id)initWithFrame:(CGRect)frame

    self = [super initWithFrame:frame];
    if (self) 
        // Initialization code
        self.imageView.frame = self.contentView.frame;
        self.imageView.backgroundColor = [UIColor yellowColor];
        DLog(@"contentView frame: %f", self.contentView.frame.size.height);
    
    return self;


-(void)setPhoto:(NSURL *)photoURL 
    self.imageView.frame = self.contentView.frame;
    DLog(@"height: %f, and width %f", self.frame.size.height, self.contentView.frame.size.width);
    self.imageView.backgroundColor = [UIColor redColor];
    [self.imageView setImageWithURL:photoURL placeholderImage:[UIImage imageNamed:@"foo.jpg"]];


@end

LazyJoe.h

#import <UIKit/UIKit.h>

@interface LazyJoe : UICollectionViewFlowLayout

@end

LazyJoe.m

#import "LazyJoe.h"

@implementation LazyJoe

- (id)init 
    if (!(self = [super init])) return nil;

    self.itemSize = CGSizeMake(104, 104);    
    return self;


@end

【问题讨论】:

阅读您的问题后,我有两件事不确定。我不太确定你的 UIImageView 是否被你的应用程序“保持活力”。弄清楚你的 UIImageView 对象会发生什么,你可能会找到答案。我还看到您使用了 AFNetworking 框架(我很喜欢它),但是您确定 ARC 设置正确了吗?如果您想要一个关于如何在 collectionCells 上放置图像的快速原型,请务必尝试本教程:appcoda.com/ios-programming-uicollectionview-tutorial!让我知道这是否对您有用:) @TotumusMaximus AFNetworking 有效,如 我是否要更改以下内容-部分中所述。如何调试您的 UIImageView 正在“保持活跃”?有什么建议吗? 在 'initwithframe' & 'setphoto' & 'cellforitem' 设置断点或日志。检查它是否为空、可访问并使用正确的数据设置。 【参考方案1】:
if (imgView != nil) 
    dispatch_async(dispatch_get_main_queue(), ^ 
        UIButton * imageButton = [UIButton buttonWithType: UIButtonTypeCustom];
        imageButton.frame = CGRectMake(-60, -60, 257, 200);
        [imageButton setImage: img forState: UIControlStateNormal];
        imageButton.tag = [selectorValue intValue];
        imageButton.adjustsImageWhenHighlighted = NO;
        [imageButton addTarget: self action: @selector(imageViewClicked: ) forControlEvents: UIControlEventTouchUpInside];
        [cell addSubview: imageButton];
    );

【讨论】:

以上是关于UICollectionViewCell 不显示图像的主要内容,如果未能解决你的问题,请参考以下文章

子类 UICollectionViewCell 不更新图像

UIModalPresentationPopover 的箭头不指向 UICollectionViewCell

具有可变图像大小和自动布局的 UICollectionViewCell

scrollToItemAtIndexPath 破坏 UICollectionViewCell 布局

将触摸事件传递给 UICollectionViewCell 内容?

NSOperationQueue mainQueue 不更新 UICollectionViewCell