Trouble accessing Image from instance of UICollectionViewCell

穿精又带淫゛_ 提交于 2019-12-10 23:23:30

问题


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.


回答1:


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




回答2:


If you are calling self.collectionView registerClass..., then you need to remove it. Storyboards handle this registration automatically.



来源:https://stackoverflow.com/questions/17655252/trouble-accessing-image-from-instance-of-uicollectionviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!