user5513630
user5513630

Reputation: 1699

How to display json file data to UICollectionView

Here I tried to get data from my URL used base64,shaa256 for authorize Header and I just tried to display in my console. It's worked well. But I need to convert to NSDictionary and to display to my UICollectionView. And my data having 3 sections with different data. Each section having 12 - 20 files.

So my question are:

  1. How to convert my url to json to NSDictionary ?
  2. Hoe to display with 3 section & each section should have 12 to 20 data files?
  3. I used only this code in my viewdidload to get data.does i need to add 1.Connection did receive response 2.Connection did receive data 3.connection didFailWithError 4.connectionDidFinishLoading to my code or don't want these 4 method to add in to my code

Here is my code:

I use to insert my below code to my above code after

NSString *str = [[NSString alloc] initWithData:returnData 
                                      encoding:NSUTF8StringEncoding];

NSError * error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:str 
                                                     options: NSJSONReadingMutableContainers 
                                                       error: &error];

NSLog(@"%@",json);

But it din't work.if any useful answer for my 3 question is very useful for me.i am new to ios.help me out.Thanks in advance !

Upvotes: 1

Views: 435

Answers (1)

emotality
emotality

Reputation: 13025

The data will most probably be JSON and not plain text? So convert the response straight to NSDictionary:

// show loading indicators when you are fetching data
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    // hide loading indicators when you received a response
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

    if (connectionError) {
        // Handle error
    } else {
        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"jsonResults: %@", jsonResults);
        // reload your views
        // [self.myCollectionView reloadData];
    }
}];

To make the collectionView with 3 sections you would need to have an NSArray containing the 3 sections, each of these 3 arrays would contain the 12-20 dictionaries with keys and values that will populate each cell.

NSArray *sectionOne = @[@{@"Section":@"1", @"Object":@"1"}, @{@"Section":@"1", @"Object":@"2"}];
NSArray *sectionTwo = @[@{@"Section":@"2", @"Object":@"1"}, @{@"Section":@"2", @"Object":@"2"}];
NSArray *sectionThree = @[@{@"Section":@"3", @"Object":@"1"}, @{@"Section":@"3", @"Object":@"2"}];
NSArray *collectionData = @[sectionOne, sectionTwo, sectionThree];

NSLog(@"collectionData: %@", collectionData);

Upvotes: 1

Related Questions