UICollectionViewCell 不显示图像 [英] UICollectionViewCell does not show images

查看:130
本文介绍了UICollectionViewCell 不显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Running the below stated code, I get the following output in the debugger. MagazineCell initWithFrame is called and when reloading the data the setPhoto is called. If I would put the setPhoto code in the initWithFrame method a reload would have no effect. But the simulator does only show grey cells. No Image added, no backgroundcolor changed to yellow/red. What do I miss?

[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.不是我想要的

I get the folloing output in the debbuger. The Images are shown, too small, and the MagazineCell is not called. Not that what I want

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

似乎 imageView 无法正常工作、地址错误或其他问题.但我没有看到我的错误......在完整代码下方:

Seems like the imageView is not working, wrong addressed, or something else. But I do not see my mistake... Below the complete code:

#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

推荐答案

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 不显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆