如何使用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];
我在debbuger中获得了以下输出。显示的图像太小,而且没有调用MagazineCell。不是我想要的
[5402:70b] -[WebApi getSurroundImages] [Line 326] do surround composition
[5402:70b] -[SurroundViewController didComposition] [Line 146] reload Collection View Data
似乎imageView无法正常工作,错误解决或其他问题。但我没有看到我的错误......在完整的代码下面:
#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
#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
#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
#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
#import <UIKit/UIKit.h>
@interface LazyJoe : UICollectionViewFlowLayout
@end
#import "LazyJoe.h"
@implementation LazyJoe
- (id)init {
if (!(self = [super init])) return nil;
self.itemSize = CGSizeMake(104, 104);
return self;
}
@end
答案 0 :(得分:0)
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];
});
}