gpt4 book ai didi

ios - 带有来自 json 的数据的水平和垂直可滚动 TableView

转载 作者:行者123 更新时间:2023-11-29 12:24:56 27 4
gpt4 key购买 nike

我想创建一个可水平和垂直滚动的 TableView 。我可以通过自己接受值(value)观来做到这一点。但我想从 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];

我是全新的,非常感谢任何答案和任何建议。请回复。

谢谢!

最佳答案

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

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

希望对其他人有帮助。

关于ios - 带有来自 json 的数据的水平和垂直可滚动 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29466440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com