我正在努力尝试一些可能很容易的事情,但我做不到!
我在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)#>];
只需在此方法中提供行号和节号即可。
然后使用该indexpath通过以下方法获取单元格
UICollectionViewCell *cell = [collectionView1 cellForItemAtIndexPath:indexPath];
然后,您可以访问单元格的属性。
关于ios - 如何在IBAction方法中调用UICollectionViewCell ImageView?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23210326/