水平和垂直滚动 table 视图,数据来自 json
Horizontally and Vertically scrollable table view with data from json
我想创建一个可水平和垂直滚动的 table 视图。我可以通过自己接受价值观来做到这一点。但我想从 json 中获取值。我正在使用模型 class,我的 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。
我正在获取 header 值和左列值。如何在适当的列中获取与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];
我是全新的,非常感谢任何答案和建议。请回复。
谢谢!
尝试了很长时间后,我使用下面的代码得到了结果:
-(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];
}
希望对其他人有所帮助。
我想创建一个可水平和垂直滚动的 table 视图。我可以通过自己接受价值观来做到这一点。但我想从 json 中获取值。我正在使用模型 class,我的 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。
我正在获取 header 值和左列值。如何在适当的列中获取与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];
我是全新的,非常感谢任何答案和建议。请回复。
谢谢!
尝试了很长时间后,我使用下面的代码得到了结果:
-(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];
}
希望对其他人有所帮助。