我发送json并将值存储在Collection视图中,数组在didSelectItemAtIndexPath显示为空请帮助我..
NSDictionary * jsonResult = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
albumImageArr = [NSMutableArray array];
[albumImageArr addObjectsFromArray:[jsonResult objectForKey:@"photoList"]];
NSLog(@"Array %@",albumImageArr);
for (NSDictionary *dic in albumImageArr)
{
str1=[dic objectForKey:@"photoURL"];
[arr addObject:str1];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView1 cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
CustomCell *cell = (CustomCell *)[collectionView1 dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSString *path = [[albumImageArr objectAtIndex:indexPath.row] objectForKey:@"photoURL"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
cell.imageView.image = image;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView1 didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PhotoDescriptionViewController *albumsController = (PhotoDescriptionViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"DescriptionView"];
NSString *senderImg=[arr objectAtIndex:indexPath.item];
albumsController.receivedImage=senderImg;
[self.navigationController pushViewController:albumsController animated:YES];
}
答案 0 :(得分:1)
您只需要执行类似于您在cellForRowAtIndexPath中所做的操作,然后将albumsController的receivedImage设置为图像(假设receivedImage是UIImage)。
- (void)collectionView:(UICollectionView *)collectionView1 didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
PhotoDescriptionViewController *albumsController = (PhotoDescriptionViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"DescriptionView"];
NSString *path = [[albumImageArr objectAtIndex:indexPath.row] objectForKey:@"photoURL"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
albumsController.receivedImage = image;
[self.navigationController pushViewController:albumsController animated:YES];
}
回应您的进一步询问: 在PhotoDescriptionViewController中将receivedImage从NSString更改为UIImage,然后在viewWillAppear :(或无论您将图像设置在何处),改为:
imageView.image = receivedImage;