0

In my iPhone app I just parse the JSON from a url and save it to arrays. After completing JSON parsing my UICollectionView should be updated with the new array values. But my UICollectionView isn't updating. Please look through my following coding

@interface SpecialPgms ()
@property (strong, nonatomic) IBOutlet UICollectionView *collecSpecial;
@end

@implementation SpecialPgms
NSMutableArray *arrTitle,*arrUrl,*arrImg;
UICollectionViewCell *cell ;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    _sidebarButton.tintColor = [UIColor colorWithWhite:0.1f alpha:0.9f];
    _sidebarButton.target = self.revealViewController;
    _sidebarButton.action = @selector(revealToggle:);
    [self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
    self.collecSpecial.delegate=self;
    self.collecSpecial.dataSource=self;
    [self loadJSON];
}


-(void)loadJSON{
    arrTitle=[[NSMutableArray alloc]init];
    arrUrl=[[NSMutableArray alloc]init];
    arrImg=[[NSMutableArray alloc]init];

    [pro startAnimating];

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
        JSONDecoder *decoder = [JSONDecoder decoderWithParseOptions:JKParseOptionStrict];


        NSData *immutableItemList = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos?author=jiljilmedia&v=2&alt=jsonc"]];

        NSData *data =immutableItemList;
        NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSData* data1 = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *returnedData = (NSArray *) [[[decoder objectWithData:data1] objectForKey:@"data"] objectForKey:@"items"];



        for(NSDictionary * student in returnedData){

            NSString *strTitle=[student objectForKey:@"title"];

            NSDictionary *Thumbnail=[student objectForKey:@"thumbnail"];
            NSString *strImg = Thumbnail[@"sqDefault"];
            NSDictionary *player = student[@"player"];
            NSString *strdefaultUrl = player[@"default"];
            NSString *strMobileUrl=player[@"mobile"];

            [arrTitle addObject:strTitle];
            //NSLog(@"%@",strImg);

            if(strMobileUrl.length==0){
                [arrUrl addObject:strdefaultUrl];
            }
            else{
                [arrUrl addObject:strMobileUrl];
            }
            [arrImg addObject:strImg];

        }
        [self performSelectorOnMainThread:@selector(setProgramTableView:) withObject:[NSArray arrayWithObjects: nil] waitUntilDone:YES];
    });

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return arrTitle.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"Cell";

    cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
    recipeImageView.image = [UIImage imageNamed:[arrImg objectAtIndex:indexPath.row]];

    UILabel *lab = (UILabel *)[cell viewWithTag:101];
    lab.text= [arrImg objectAtIndex:indexPath.row];

    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"photo-frame.png"]];




    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
       NSLog(@"%d",indexPath.row);
}

////// JSON Parsing for video links////////////////////
- (void)setProgramTableView: (NSArray*)array
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [  self.collecSpecial reloadData];
        [  self.collecSpecial.collectionViewLayout invalidateLayout];
    });

    [pro stopAnimating];
}

enter image description here

David Berry
  • 39,492
  • 12
  • 80
  • 91
Kalai
  • 459
  • 2
  • 7
  • 20

0 Answers0