Question

I'm struggling with something that might be easy but I can't get to it!

I have a UIImageView inside a UICollectionViewCell and a Button Outside the UICollectionView. In my IBAction method of the button, how do I call this UIImageView?

cell.ImageView is not permitted.

EDITED CODE:

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return 10;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

static NSString *cellIdentifier = @"Cell";
VestimentaDetailCell *cell = (VestimentaDetailCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

[cell.activityIndicator startAnimating];

cell.imageFile.image = [UIImage imageNamed:@"LoadLook.png"];

PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:@"image_%ld", (long)indexPath.row]];

[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error && data.length > 0) {

cell.imageFile.image = [UIImage imageWithData:data];

    UITapGestureRecognizer *infoLook = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];

    infoLook.numberOfTapsRequired = 1;

    [cell.imageFile addGestureRecognizer:infoLook];

} else {

    [cell.progressView removeFromSuperview];

        }

} progressBlock:^(int percentDone) {
    float percent = percentDone * 0.02;
    [cell.progressView setProgress:percent];
    if (percentDone == 100){
        [cell.progressView removeFromSuperview];
    };

    [cell.activityIndicator stopAnimating];
}];

return cell;
}

Action Method:

-(void)handleTap:(UITapGestureRecognizer *) infoLook{

if ([sender isSelected]) {
    [sender setImage:[UIImage imageNamed:@"Like.png"] forState:UIControlStateNormal];
    [sender setSelected:NO];
} else {
    [sender setImage:[UIImage imageNamed:@"Liked.png"] forState:UIControlStateSelected];
    [sender setSelected:YES];
UIImageView *like = [[UIImageView alloc] initWithFrame:CGRectMake(120, 220, 100, 100)];
like.image = [UIImage imageNamed:@"Love.png"];
[self.view addSubview:like];
[UIView animateWithDuration:2 animations:^{like.alpha = 0.0;}];

This part is where I can't call the UIImageView of the UICollectionViewCell:

    NSData* data = UIImageJPEGRepresentation(cell.imageFile.image, 0.8f);
    PFFile *likedImage = [PFFile fileWithName:@"Image.jpg" data:data];

    [likedImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            PFObject *userLikes = [PFObject objectWithClassName:@"UserProfile"];
            [userLikes setObject:likedImage forKey:@"likedLook"];

            userLikes.ACL = [PFACL ACLWithUser:[PFUser currentUser]];

            PFUser *user = [PFUser currentUser];
            [userLikes setObject:user forKey:@"user"];

            [userLikes saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    NSLog(@"Saved");
                } else {
                    NSLog(@"Error: %@%@", error, [error userInfo]);
                }
            }];
        }
    }];

NSLog(@"Liked Image");

}

}
Was it helpful?

Solution 6

For anyone who needs help this is what I did:

Create a Class for the Cell.

Post the code inside the Cell Class implementation file. In my case:

#import "VestimentaDetailCell.h"

@implementation VestimentaDetailCell

@synthesize imageFile, progressView;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
}
return self;
}

- (IBAction)likeLook:(id)sender {

    if ([sender isSelected]) {
        [sender setImage:[UIImage imageNamed:@"Like.png"] forState:UIControlStateNormal];
        [sender setSelected:NO];

    } else {
        [sender setImage:[UIImage imageNamed:@"Liked.png"] forState:UIControlStateSelected];
        [sender setSelected:YES];

        UIImageView *like = [[UIImageView alloc] initWithFrame:CGRectMake(120, 220, 100, 100)];
        like.image = [UIImage imageNamed:@"Love.png"];
        [self addSubview:like];
        [UIView animateWithDuration:2 animations:^{like.alpha = 0.0;}];

        NSData *imageData = UIImageJPEGRepresentation(imageFile.image, 1);
        [self uploadImage:imageData];

        NSLog(@"Liked Image");

}
}

-(void)uploadImage:(NSData *)imageData {

    PFFile *likedImage = [PFFile fileWithName:@"Image.jpg" data:imageData];

    [likedImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            PFObject *userLikedPhoto = [PFObject objectWithClassName:@"UserLikedPhoto"];
            [userLikedPhoto setObject:likedImage forKey:@"likedLook"];

            userLikedPhoto.ACL = [PFACL ACLWithUser:[PFUser currentUser]];

            PFUser *user = [PFUser currentUser];
            [userLikedPhoto setObject:user forKey:@"User"];

            [userLikedPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    NSLog(@"Saved");
                } else {
                    NSLog(@"Error: %@%@", error, [error userInfo]);
                }
            }];
        } else {
            NSLog(@"Error: %@%@", error, [error userInfo]);
        }
    }];
}

@end

OTHER TIPS

First you will have to get the indexpath of the item by using method

NSIndexPath *indexpath=[NSIndexPath indexPathForItem:<#(NSInteger)#> inSection:<#(NSInteger)#>];

Just provide the row and section number in this method. Then use that indexpath to get the cell using below method

UICollectionViewCell *cell = [collectionView1 cellForItemAtIndexPath:indexPath];

Then you can access the properties of the cell.

Now that you have your button as a part of the cell, you can do:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"cellID";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    // ... your code here for cellForItemAtIndexPath: method
    likeButton.tag = indexPath.row;
    [cell.likeButton addTarget:self action:@selector(liked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void) liked:(UIButton *)button {
    UIImage *image = [imageArray objectAtIndex:button.tag];
    NSData* data = UIImageJPEGRepresentation(image, 0.8f);
    PFFile *likedImage = [PFFile fileWithName:@"Image.jpg" data:data];

    [likedImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            PFObject *userLikes = [PFObject objectWithClassName:@"UserProfile"];
            [userLikes setObject:likedImage forKey:@"likedLook"];

            userLikes.ACL = [PFACL ACLWithUser:[PFUser currentUser]];

            PFUser *user = [PFUser currentUser];
            [userLikes setObject:user forKey:@"user"];

            [userLikes saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                    NSLog(@"Saved");
                } else {
                    NSLog(@"Error: %@%@", error, [error userInfo]);
                }
            }];
        }
    }];
}

This has not been tested though the idea is there.

It is always preferred to manipulate/retrieve data from an array that populates your collection view (such as the array of images or strings that you access by index) rather than accessing its cell.

Try this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView1 cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
        UICollectionViewCell *cell=[collectionView1 dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
        cell.backgroundView = [[UIImageView alloc] initWithImage:@"image.jpg"];
        cell.backgroundView.contentMode = UIViewContentModeScaleAspectFit;
        cell.backgroundColor=[UIColor blackColor];

        return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [your_image_array count];
}

To detect selected image:

  - (void)collectionView:(UICollectionView *)collectionView
    didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"Selected Image = %d",indexPath.row);
    }
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        tapGesture.numberOfTapsRequired = 1;
imgView.tag=indexPath.row
[imgView setUserInteractionEnabled:YES];
        [imgView addGestureRecognizer:tapGesture];


    - (void)handleSingleTap:(UITapGestureRecognizer *)tap
{
    NSLog(@"%d",[tap view].tag);
}

You can create a property

@property(strong, nonatomic) UIImage *imageForCell;

Then initialise it with the first image

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageForCell = [UIImage imageNamed:@"myImage.png"];
}

In your button you should change the image first and then reload specific cells (in this example cell 5):

- (IBAction)buttonTapped:(id)sender {
    self.imageForCell = [UIImage imageNamed:@"newImage.png"];
    NSIndexPath indexPath = [NSIndexPath indexPathForRow:5 inSection:0];
    [self.myCollectionView reloadItemsAtIndexPaths:indexPath];
}

When populating the cell use the property imageForCell:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ...
    cell.imageFile.image = self.imageForCell;
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top