Thursday, 25 August 2016

json parsing in iOS with table view

USE CASE - 1
        |
Main
        |
UITable View Controller
        |
UITable View
        |
UITable View Cell
       |
Components

1) Create UITable View
2) Add below Code in "MyTableViewController.h"

#import <UIKit/UIKit.h>

@interface MyTableViewController : UITableViewController
{
    NSMutableArray *arr1 ;
    NSMutableArray *arr2;
}

@end


3) Add below Code in "MyTableViewController.m"


- (void)viewDidLoad
{
   
    arr1=[[NSMutableArray alloc]init];
    
    arr2=[[NSMutableArray alloc]init];
    NSString *strUrl = @"http://www.jsonurl";
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
    
    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueuecompletionHandler:^(NSURLResponse*response, NSData *data, NSError *error)
     {
         if(data != nil)
         {
             NSMutableArray *dictionaryObj = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:&error];
             
             
             //            NSArray *allValues = [dictsongs allValues];
             //             NSMutableArray *arrKeys = [[NSMutableArray alloc]initWithObjects:@"image",@"title",@"song_url", nil];
             
             for (int i = 0; i<dictionaryObj.count; i++) {
                 
                 
                 [arr1 addObject:[[dictionaryObjobjectAtIndex:i]objectForKey:@"Title"]];
                 [arr2 addObject:[[dictionaryObjobjectAtIndex:i]objectForKey:@"MedImageUrl"]];
                 
                 
                 NSLog(@"array at %@",arr1[i]);
                 
             }
             NSLog(@"images list %@",arr1);
             [self.tableView reloadData];
         }
         
         else
         {
             UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"" message:@"Server Down.Try again later"delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nilnil];
             [alert show];
             
             
         }
     }];
    
    
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return arr1.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MyCell";
    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifierforIndexPath:indexPath];
    
    cell.textLabel.text=[arr1 objectAtIndex:indexPath.row];
    
    NSData  *imgData=[NSData dataWithContentsOfURL:[NSURLURLWithString:[arr2 objectAtIndex:indexPath.row]]];
    cell.imageView.image=[UIImage imageWithData:imgData];
    
    return cell;
}



No comments:

Post a Comment