【发布时间】:2014-04-29 12:15:55
【问题描述】:
我正在努力解决一些可能很容易但我无法做到的事情!
我在 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");
}
}
【问题讨论】:
-
你能更详细地解释一下“调用 UIImageView”是什么意思吗?这不是正确的术语。
-
如果我想更改位于 UICollectionViewCell 内的 UIImageView 的图像,我将如何在 IBAction 方法中设置我的代码?在这种方法中..我怎么能告诉它改变图像 cell.imageFile 是不允许的,因为它不会识别什么是单元格。
-
你没有。您不应该尝试访问单元格中的图像视图,而应该访问用于填充单元格的基础数据。您应该有一个数组,其中包含用于填充图像视图的图像(或图像名称)。
-
点击按钮后,我想更改图像。
-
您希望每个单元格中的所有图像都相同吗?如果不是,您如何确定要更改哪个单元格?
标签: ios uiimageview uicollectionviewcell ibaction