0

I'm struggling to figure out what i am doing wrong. I am basically trying to populate the my UITableView with json data. In the console I can see the results but just don't know why the data is not displayed in the tableview. I have looked at similar questions and answers but none is a solution to my problem.

Advice or help please;

#pragma mark - Private method implementation

-(void)loadData{
    // Form the query.
    @try {


        NSString *get =[[NSString alloc] initWithFormat:@""];
        NSString *getRegions = [NSString stringWithFormat:@"JSON URL HERE",self.sessionId, self.companyId];
        NSURL *url=[NSURL URLWithString:getRegions];
        NSLog(@"Get regions: %@", url);
        NSData *postData = [get dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"GET"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setHTTPBody:postData];

        //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

        NSError *error = [[NSError alloc] init];
        NSHTTPURLResponse *response = nil;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        NSLog(@"Reponse code: %ld", (long)[response statusCode]);
        if ([response statusCode] >= 200 && [response statusCode] < 300)
        {
            NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
            NSLog(@"Response ==> %@", responseData);
            @try{
                NSError *error = nil;
                regionsJson = [[NSMutableArray alloc]init];
                //[jsonData removeAllObjects];
                regionsJson = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error];

                NSArray *tblArray = [NSArray arrayWithObject:regionsJson];
            }
            @catch (NSException *e){
                NSLog(@"Try catch block: %@", e);
            }
            @finally{
                // [self.tblRegion reloadData];
                NSLog(@"finally");
            }


            structureJson =[regionsJson valueForKey:@"structure"];
            companyJson =[structureJson valueForKey:@"company"];
            _barBtnCompanyName.title = [companyJson valueForKey:@"company_name"];
            NSLog(@"Get company name: %@", [companyJson valueForKey:@"company_name"]);

            for  (int i =0 ; i < regionsJson.count; i++){
                regionsJson = [companyJson objectForKey:@"regions"];
                NSString *regionName = [NSString stringWithFormat:@"%@", [regionsJson valueForKey:@"region_name"]];
                NSLog(@"Region name: %@",regionName);
                // [regionsJson addObject:regionName];
                 NSString *alarmCount = [NSString stringWithFormat:@"%@", [regionsJson valueForKey:@"alarm_cnt"]];
                 NSLog(@"Alarm count: %@", alarmCount);
                 // [regionsJson addObject:alarmCount];

             }

        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);

    }
    // Reload the table view.
    [self.tblRegion reloadData];

}


#pragma mark - UITableView method implementation

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"Number of rows: %lu", (unsigned long)regionsJson.count);
    return [regionsJson count];

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"CellRegions";
    // Dequeue the cell.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    // Set the loaded data to the appropriate cell labels.
     cell.textLabel.text = [[regionsJson objectAtIndex:indexPath.row] objectForKey:@"region_name"];
     cell.detailTextLabel.text = [[regionsJson objectAtIndex:indexPath.row] objectForKey:@"alarm_cnt"];

    [cell setAccessoryType: UITableViewCellAccessoryDisclosureIndicator];
    NSLog(@"Table cell: %@", cell);
    return cell;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Cockpit Aliens
  • 411
  • 6
  • 18
  • Try to `[self.tblRegion reloadData];` in main thread. – Larme Mar 05 '15 at 14:56
  • I have tried that still nothing is displayed in the tableview. -(void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view. [self.tblRegion reloadData]; [self loadData]; } – Cockpit Aliens Mar 05 '15 at 14:59
  • I main thread, once you got the JSON data with `dispatch_get_main_queue` – Larme Mar 05 '15 at 15:00
  • please post your answer. i'd appreciate it because i'm still learning objective-c i don't know dispatch_get_main_queue. – Cockpit Aliens Mar 05 '15 at 15:02
  • http://stackoverflow.com/questions/4968424/tableview-reloaddata-doesnt-work-until-i-scroll-the-tableview – Larme Mar 05 '15 at 15:04
  • Both the object NSLogs for region_name and alarm_cnt show parsed results in the console but if i uncomment the [regionsJson addObject:regionName] and [regionsJson addObject:alarmCount] the app don't parse results. – Cockpit Aliens Mar 05 '15 at 15:05
  • I added in in loadData still nothing is displayed in that tableview dispatch_async(dispatch_get_main_queue(), ^{ [self.tblRegion reloadData]; }); – Cockpit Aliens Mar 05 '15 at 15:11
  • Your call to `reloadData` needs to be at the end of the `try` block, after you process all of the data, not outside the whole `try/catch` block. – rmaddy Mar 05 '15 at 15:14
  • I've put the reload inside the try catch but still no data displayed in the table. Do I need to u comment the regionName addobjects to pass the values to the table or what? – Cockpit Aliens Mar 05 '15 at 15:21

1 Answers1

0

My code is just fine, i did a stupid omission of this declaration.

-(void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tblRegion.delegate = self;
self.tblRegion.dataSource = self;
 [self loadData];

}

Cockpit Aliens
  • 411
  • 6
  • 18