Reputation: 8894
I'm following this tutorial with some modifications for my project: http://www.appcoda.com/ios-programming-uicollectionview-tutorial/
I'm trying to get the instance of UIImageView that IB creates for me.
Here is a screenshot of my IB:
I have a custom class called FeedViewCell that is to contain an UIImageView. Here is the cellForItemAtIndexPath code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:100];
imageView.image = [UIImage imageNamed:[postPhotos objectAtIndex:indexPath.row]];
return cell;
}
The problem is that UIImageView *imageView = (UIImageView *)[cell viewWithTag:100]; comes back as nil. Using the viewWIthTag method seems weird to me anyway, but in the debugger I see no sign that imageView is a subview of UICollectionViewCell. If you look at this debug screen, you can see that the cell doesn't appear to have any UIImageView subviews:
However I see 2 UIImageViews as subviews of the CollectionView.
So it seems to be that I'm doing something wrong in IB. This isn't surprising as I always seem to struggle with IB (looking at code at least I can see what's going on).
Thanks for any suggestions!
update: I gave up on using IB to hook in the ImageView and tried creating it in code as below: http://cl.ly/QFxs
The images don't display properly. If you look in the screenshot (debugger) though, you will see images and imageViews are both valid objects though.
Upvotes: 2
Views: 685
Reputation: 9040
If you are calling self.collectionView registerClass...
, then you need to remove it. Storyboards handle this registration automatically.
Upvotes: 1
Reputation: 3616
I don't think you need to use tags in this situation. You can create a UIImageView property in FeedViewCell wire it up in interface builder and then access it in
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
For Example
// in the FeedViewCell
@interface FeedViewCell : UICollectionViewCell
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
// in the controller
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
FeedViewCell *cell = (FeedViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:[postPhotos objectAtIndex:indexPath.row]];
return cell;
}
Open up storyboard then click the assistant editor button which will bring up the another window. Open the feedViewCell.h there and ctrl+click on the imageView and drag it to the .h file that will give you a menu to create the outlet. You can give it the name to the imageView Property. That should be it.
Check out this link for more info http://klanguedoc.hubpages.com/hub/IOS-5-A-Beginners-Guide-to-Storyboard-Connection
Upvotes: 1