NSInvalidArgumentException原因:尝试显示Flickr图像时,UITableView中的数据参数为nil

时间:2014-05-21 06:31:56

标签: ios uitableview flickr

您好,在我的应用程序中,我想在Flickr中显示UITableView相册列表,所以我搜索了很长时间,并找到了一些解决方案。我已经使用了解决方案中给出的方法,它没有像

那样给出错误
NSInvalidArgumentException', reason: 'data parameter is nil

解决方案link click here

因为我第一次尝试这个,所以我无法解决这个问题。这是MY API LINK for Flickr

我已使用此代码在UItableview

中显示Flickr图像相册列表
{
   NSMutableArray *photoURLs;
   NSMutableArray *photoSetNames;
   NSMutableArray *photoid1;
}

我的Flickr API密钥

#define  FlickrAPIKey @"a6a0c7d5efccffc285b0fe5ee1d938e3"

  - (void)viewDidLoad
{
    [super viewDidLoad];
     photoURLs = [[NSMutableArray alloc] init];
     photoSetNames = [[NSMutableArray alloc] init];
     photoid1 = [[NSMutableArray alloc] init];
    [self loadFlickrPhotos];

}

我的TableView代码

  - (void)loadFlickrPhotos
 {
      NSString *urlString = [NSString stringWithFormat:@"http://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=%@&user_id=%@&per_page=10&format=json&nojsoncallback=1", FlickrAPIKey, @"124757153@N04"];
      NSURL *url = [NSURL URLWithString:urlString];

      NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

      NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
      NSArray *photosets = [[results objectForKey:@"photosets"] objectForKey:@"photoset"];
   for (NSDictionary *photoset in photosets) {

       NSString *title = [[photoset objectForKey:@"title"] objectForKey:@"_content"];
      [photoSetNames addObject:(title.length > 0 ? title : @"Untitled")];
       NSString *photoid = [photoset objectForKey:@"id"];
      [photoid1 addObject:photoid];

     }
  }
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

      return [photoSetNames count];
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     static NSString *cellIdentifier =@"Cell";

     flickrpoliticalCell *cell =(flickrpoliticalCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil) {

       cell = [[flickrpoliticalCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
     }
    cell.tit.text = [photoSetNames objectAtIndex:indexPath.row];
    return cell;
}

1 个答案:

答案 0 :(得分:1)

试试这个

- (void)loadFlickrPhotos
{

//
NSString *urlString = [NSString stringWithFormat:@"https://www.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=a6a0c7d5efccffc285b0fe5ee1d938e3&format=json&user_id=124757153@N04&per_page=10&nojsoncallback=1",nil];

NSLog(@"the url string==%@",urlString);
NSURL *url = [NSURL URLWithString:urlString];

NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

NSLog(@"the str==%@",jsonString);

NSDictionary *results = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil];
   NSArray *photosets = [[results objectForKey:@"photosets"] objectForKey:@"photoset"];


for (NSDictionary *photoset in photosets) {

    NSString *title = [[photoset objectForKey:@"title"] objectForKey:@"_content"];
    NSLog(@"title==%@",title);
    [photoSetNames addObject:(title.length > 0 ? title : @"Untitled")];

     NSString *primary = [photoset objectForKey:@"primary"];

    NSString *server = [photoset objectForKey:@"server"];

     NSString *secret = [photoset objectForKey:@"secret"];

    NSString *farm = [photoset objectForKey:@"farm"];

    NSString *urlstr=[NSString stringWithFormat:@"http://farm%@.staticflickr.com/%@/%@_%@.jpg",farm,server,primary,secret];

     NSLog(@"your photo id==%@",urlstr);

    [photoids addObject:urlstr];





}
}