gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:43:45 25 4
gpt4 key购买 nike

我正在为一些可能很容易但做不到的事情而苦苦挣扎!

我在 UICollectionViewCell 内有一个 UIImageView,在 UICollectionView 外有一个按钮。在按钮的 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");

}

}

最佳答案

首先,您必须使用方法获取项目的索引路径

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

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

UICollectionViewCell *cell = [collectionView1 cellForItemAtIndexPath:indexPath];

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

关于ios - 如何在 IBAction 方法中调用 UICollectionViewCell ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23210326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com