【问题标题】:CollectionView with JSON Data带有 JSON 数据的 CollectionView
【发布时间】:2014-11-02 16:24:57
【问题描述】:

我正在发送 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];

}

【问题讨论】:

  • 请说明您进行了哪些调试以尝试解决此问题。
  • 我在 cellForRowAtIndexPath 中显示图像数据。我想用 didSelectItemAtIndexPath 在detailedViewController 中显示图像数据。
  • 那么,你在 didSelectItemAtIndexPath 中访问相同的数据结构吗?不要指望从单元格中获取数据,从你的数据结构中获取。

标签: ios iphone json ios7 uicollectionview


【解决方案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;

【讨论】:

  • 它显示这样的警告不兼容的指针类型从 'uiimage *' 分配给 'nsstring *'
  • albumsController.receivedImage 是什么类型的?
  • receivedImage 是 NSString 类型
  • 正如我的回答中提到的,我希望它是一个 UIImage,因为它就是它正在通过的。
  • 我想显示图像,但我将接收图像作为字符串
猜你喜欢
  • 2018-12-04
  • 2016-09-03
  • 2011-03-29
  • 1970-01-01
  • 1970-01-01
  • 2019-11-19
  • 2011-04-23
  • 2016-09-24
相关资源
最近更新 更多