具有来自json的数据的水平和垂直可滚动表视图

时间:2015-04-06 06:29:02

标签: ios xcode6 asihttprequest sbjson

我想创建一个可以水平和垂直滚动的表格视图。我可以通过自己获取价值来做到这一点。但是我想从json中获取值。我正在使用模型类,我的json值是这样的:

[

{

    "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"

},

我希望在水平滚动时修复第一列,在垂直滚动时修复第一行。第一栏应该有iid。第一行应该有yr,数据应该有val。

我正在使用XCMltisortTableView,ASIHttpRequest和SBJson。

我正在获取标题值和左列值。如何在正确的列中获取与yr和iid相对应的数据?

我在requestFinished方法中的val代码如下:

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];

我是全新的,会欣赏任何答案和任何建议。请回复。

谢谢!

1 个答案:

答案 0 :(得分:1)

经过很长一段时间的尝试后,我使用下面的代码得到了结果:

-(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];
}

希望它可以帮助他人。