如何在IBAction方法中调用UICollectionViewCell ImageView?

时间:2014-04-22 04:22:32

标签: ios uiimageview uicollectionviewcell ibaction

我正在努力做一些可能很容易的事情,但我无法做到!

我在UICollectionViewCell中有一个UIImageView,在UICollectionView外面有一个Button。在按钮的IBAction方法中,如何调用此UIImageView?

不允许使用cell.ImageView。

编辑代码:

-(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;
}

行动方法:

-(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;}];

这部分是我无法调用UICollectionViewCell的UIImageView的地方:

    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");

}

}

6 个答案:

答案 0 :(得分:1)

首先,您必须使用方法

获取项目的索引路径
NSIndexPath *indexpath=[NSIndexPath indexPathForItem:<#(NSInteger)#> inSection:<#(NSInteger)#>];

只需在此方法中提供行号和节号。 然后使用该索引路径使用以下方法获取单元格

UICollectionViewCell *cell = [collectionView1 cellForItemAtIndexPath:indexPath];

然后您可以访问单元格的属性。

答案 1 :(得分:1)

现在您已将按钮作为单元格的一部分,您可以执行以下操作:

-(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]);
                }
            }];
        }
    }];
}

虽然这个想法存在,但尚未经过测试。

始终首选从填充集合视图的数组(例如通过索引访问的图像或字符串数​​组)操作/检索数据,而不是访问其单元格。

答案 2 :(得分:0)

试试这个:

- (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];
}

检测所选图像:

  - (void)collectionView:(UICollectionView *)collectionView
    didSelectItemAtIndexPath:(NSIndexPath *)indexPath
    {
        NSLog(@"Selected Image = %d",indexPath.row);
    }

答案 3 :(得分:0)

    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);
}

答案 4 :(得分:0)

您可以创建属性

@property(strong, nonatomic) UIImage *imageForCell;

然后用第一张图片

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

在按钮中,您应首先更改图像,然后重新加载特定单元格(在此示例中为单元格5):

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

填充单元格时,请使用属性imageForCell:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ...
    cell.imageFile.image = self.imageForCell;
    ...
}

答案 5 :(得分:0)

对于任何需要帮助的人来说,这就是我所做的:

为单元格创建一个类。

将代码发布在Cell Class实现文件中。 就我而言:

#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