在Collection View上显示JSON解析

时间:2013-05-20 16:12:50

标签: ios json parsing uicollectionview

我正在尝试显示来自Web服务的已解析图像,但我的图像未显示。我意识到在视图加载后图像被添加到数组中。那么,我是否可以在集合视图中显示图像?更好的是,应该在主线程上执行哪一行代码,以便加载图像?谢谢你的帮助。

#import "ShowImagesCollectionViewController.h"
#import "ImageCell.h"
@interface ShowImagesCollectionViewController (){
        NSMutableArray *array;
}

@property(nonatomic, strong)NSURLConnection *connection;
@property(nonatomic, strong)NSMutableData *webdata;
@end

@implementation ShowImagesCollectionViewController

- (void)viewDidLoad
      {
      [super viewDidLoad];
      [self getData];
      array = [[NSMutableArray alloc]initWith];
      [_collectionView reloadData];
       }



 -(void)getData
       {
       [_activyIndicator startAnimating];
        NSURL *url = [NSURL URLWithString:@"WEBSITE"];
       NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];

       _connection = [NSURLConnection connectionWithRequest:requestURL delegate:self];
        if (_connection) {
       _webdata = [[NSMutableData alloc]init];
       }

        }

连接

   -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
        {
        [_webdata setLength:0];
        }
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
         {
        [_webdata appendData:data];

          }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
        {
         NSLog(@"Fail With error");
         }
        -(void)connectionDidFinishLoading:(NSURLConnection *)connection
         {
            [_activyIndicator startAnimating];
           NSDictionary *allDataDictionary =[NSJSONSerialization JSONObjectWithData:_webdata   options:kNilOptions  error:nil];
           NSDictionary *currentShowDictionary = [allDataDictionary objectForKey:@"Current_Show"];

            NSArray *photoArray =[currentShowDictionary objectForKey:@"Photos"];
            for (NSDictionary *diction in photoArray) {
            NSDictionary *photo_urlDictionary = [diction objectForKey:@"photo_url"];
           // NSLog(@"%@",photo_urlDictionary);
           NSURL *collectionURL = [NSURL URLWithString:[photo_urlDictionary objectForKey:@"url"]];




            NSData *colectionImageData = [NSData dataWithContentsOfURL:collectionURL];

            UIImage *collectionImage = [UIImage imageWithData:colectionImageData];
            [array addObject:collectionImage]; 
            NSLog(@"%@",array);


             }

            [_activyIndicator stopAnimating];

             }

的CollectionView

 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
              return 1;
          }
         -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
                {
                return [array count];
                 }
        -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
          {
                    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureCell" forIndexPath:indexPath];
                    NSString *myImageString = [array objectAtIndex:indexPath.row];
                    cell.pictureImageView.image = [UIImage imageNamed:myImageString];

                    return cell;
          }

1 个答案:

答案 0 :(得分:0)

您在加载视图时调用[_collectionView reloadData],但在图像的异步加载完成时不再调用-(void)connectionDidFinishLoading:(NSURLConnection *)connection。如果你在{{1}}结束时进行此调用,它应该可以解决问题。