How do I get the image shown in the cell, using UICollectionViewCell?
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
Do I change the following things
// in viewDidLoad
// [self.collectionView registerClass:[MagazineCell class]
// in - collectionView:cellForItemAtIndexPath:indexPath
[mCell.imageView setImageWithURL:photoURL placeholderImage:[UIImage imageNamed:@"foo.jpg"]];
// [mCell setPhoto:photoURL];
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
Seems like the imageView is not working, wrong addressed, or something else. But I do not see my mistake... Below the complete code:
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