Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 绑定UICollectionView与rest api数据问题_Ios_Api_Rest_Uicollectionview - Fatal编程技术网

Ios 绑定UICollectionView与rest api数据问题

Ios 绑定UICollectionView与rest api数据问题,ios,api,rest,uicollectionview,Ios,Api,Rest,Uicollectionview,我正在尝试从外部API获取图像,并将其绑定到该视图中的UICollectionView&UIImageView单元格。我能够获取数据并将其打印在日志文件中。但是,我无法在UICollectionView上看到图像。下面是我的数据绑定的代码 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { I

我正在尝试从外部API获取图像,并将其绑定到该视图中的UICollectionView&UIImageView单元格。我能够获取数据并将其打印在日志文件中。但是,我无法在UICollectionView上看到图像。下面是我的数据绑定的代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   ImagesViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

  // imagesArray is an array with serialized json data.

  NSDictionary *finalImages = [self.imagesArray objectAtIndex:indexPath.row];

  NSLog(@"Entering collection view.....");

  [[cell imageViewCell]setImage:[UIImage imageNamed:[finalImages valueForKey:@"link"]]];

  return cell;
}
is数据是JSON格式的

 data
{
  abc: 'abc',
  xyz: 'xyx',
  link: 'link to an online image'
}

UIImage ImageName:
采用文件名,而不是URL

在显示图像文件之前,需要手动将其加载到
NSData
对象中

NSData dataWithContentsOfURL
将为您执行此操作

UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[finalImages valueForKey:@"link"]]];

imageNamed是从包(image.png)中的图像文件获取图像的方法

如果您想从URL获取图像,请将其作为Richhard Brown先生的答案下载或使用SDWebImage(直接使用imageView设置)


示例colectionView SDWebImage代码(nonARC):

非常感谢您的回复。这很有道理。但是,我试图打印一条日志消息并查看日志文件中的数据。它不会在控制台中打印任何内容。看起来它一开始并没有启动这项活动。我来自.net背景,所以请原谅我的术语。-(UICollectionViewCell*)collectionView:(UICollectionView*)cv cellForItemAtIndexPath:(NSIndexPath*)indexPath由于某种原因未触发。我尝试在该事件中使用日志,但它不会在我的控制台中打印。确实要为collectionView设置datasource或将datasource拖到viewController吗?很抱歉,回复太晚,但aaa&提供的示例对于找出问题所在非常有用。谢谢你们。