iosxcode6asihttprequestsbjson

Horizontally and Vertically scrollable table view with data from json


I want to create a table view which is scrollable both horizontally and vertically. I can do this by taking values myself. But I want to take the values from json. I am using model class and my json values are like this:

[

{

    "cid": 109,
    "iid": 10653,
    "yr": 1994,
    "val": "15.4311527806175"

},
{

    "cid": 109,
    "iid": 7872,
    "yr": 1999,
    "val": "8.84575553637328"

},
{

    "cid": 109,
    "iid": 7872,
    "yr": 1998,
    "val": "6.18441582677751"

},

I want my first column to be fixed when scrolling horizontally and first row to be fixed when I scroll it vertically. First column should have iid. first row should have yr and data should have val.

I am using XCMltisortTableView, ASIHttpRequest and SBJson.

I am getting the header value and left column value. How can I get the data corresponding to the yr and iid in proper column?

My code for val in requestFinished method is as follows:

rightTableData = [NSMutableArray arrayWithCapacity:mainTableData.count];
NSMutableArray *right = [NSMutableArray arrayWithCapacity:mainTableData.count];
for (int i = 0; i < mainTableData.count; i++)
{
//        Model *model = [mainTableData objectAtIndex:i];

    NSMutableArray *array = [NSMutableArray arrayWithCapacity:mainTableData.count];
    for (int j = 0; j < headData.count; j++)
    {
        Model *model = [mainTableData objectAtIndex:j];

        if([headData isEqual: @"2000"])
        {
            [array addObject:[NSString stringWithFormat:@"%@", model.val]];
        }
        else if([headData isEqual: @"2001"])
        {
            [array addObject:[NSString stringWithFormat:@"%@", model.val]];
        }
        else if([headData isEqual: @"2002"])
        {
            [array addObject:[NSString stringWithFormat:@"%@", model.val]];
        }
        else if([headData isEqual: @"2003"])
        {
            [array addObject:[NSString stringWithFormat:@"%@", model.val]];
        }
        else if([headData isEqual: @"2004"])
        {
            [array addObject:[NSString stringWithFormat:@"%@", model.val]];
        }
        else
        {
            [array addObject:[NSString stringWithFormat:@"%@", model.val]];
        }

    }
    [right addObject:array];
}
[rightTableData addObject:right];

I'm totally new and would a appreciate any answers and any suggestions. Please do reply.

Thanks!


Solution

  • After trying for a long time I got the result using the code below:

    -(void) requestFinished: (ASIHTTPRequest *) request
    {
        NSString *theJSON = [request responseString];
    
        SBJsonParser *parser = [[SBJsonParser alloc] init];
    
        NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];
    
        headData = [[NSMutableArray alloc] init];
        NSMutableArray *head = [[NSMutableArray alloc] init];
    
        leftTableData = [[NSMutableArray alloc] init];
        NSMutableArray *left = [[NSMutableArray alloc] init];
    
        rightTableData = [[NSMutableArray alloc]init];
    
        for (NSMutableArray *dictionary in jsonDictionary)
        {
            Model *model = [[Model alloc]init];
    
            model.cid = [[dictionary valueForKey:@"cid"]intValue];
            model.iid = [[dictionary valueForKey:@"iid"]intValue];
            model.yr = [[dictionary valueForKey:@"yr"]intValue];
            model.val = [dictionary valueForKey:@"val"];
    
            [mainTableData addObject:model];
    
            [head addObject:[NSString stringWithFormat:@"%ld", model.yr]];
            [left addObject:[NSString stringWithFormat:@"%ld", model.iid]];
        }
        NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:head];
        headData = [[orderedSet array] mutableCopy];
    
        NSOrderedSet *orderedSet1 = [NSOrderedSet orderedSetWithArray:left];
        NSMutableArray *arrLeft = [[orderedSet1 array] mutableCopy];
    
        //remove duplicate enteries from header array
        [leftTableData addObject:arrLeft];
    
    
        NSMutableArray *right = [[NSMutableArray alloc]init];
        for (int i = 0; i < arrLeft.count; i++)
        {
            NSMutableArray *array = [[NSMutableArray alloc] init];
            for (int j = 0; j < headData.count; j++)
            {
                /* NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld", [[arrLeft objectAtIndex:i] intValue]];
                 NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];*/
                 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld AND SELF.yr == %ld", [[arrLeft objectAtIndex:i] intValue], [[headData objectAtIndex:j] intValue]];
                 NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];
    
                if([filteredArray count]>0)
                {
                    Model *model = [filteredArray objectAtIndex:0];
                    [array addObject:model.val];
                }
            }
            [right addObject:array];
        }
        [rightTableData addObject:right];
    }
    

    Hope it helps others.