0

嗨,我是编程新手。

我对 NSDictionary 和 Table 单元格有一些问题。我试图找到好的教程,但没有找到任何可以帮助我的东西。我正在使用 JSONKit 来解析 json。我有旧版本的 xcode,只有模拟器 4.3。

我想做的是将我的 Json 解析成一个带有单元格的表格。

我要解析的json:

{[{"n_id":"1","name":"Green","age":"23"}]}

我的主要问题。我没有收到任何错误,但是当我运行该应用程序时,它会自动关闭。当我注释掉 //cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 并将其替换为 cell.textLabel.text = @"Test"; 该应用程序和输出工作正常。我也能得到 NSLog(); 价值观

我认为我的错误在 cell.textLabel.text = [studName objectAtIndex:indexPath.row]; 提前致谢

//Json2ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];



NSData * JSONdata =[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://myurl.php"]];

 if([JSONdata length] == 0){
 [JSONdata release];
 return;
 }

NSArray *students =[JSONdata objectFromJSONData];

studName = [[NSMutableArray alloc] init];

for(NSDictionary *student in students)
{
    NSLog(@"Name: %@", [student objectForKey:@"name"]);
    NSLog(@"Age: %@", [student objectForKey:@"age"]);

    content = [student objectForKey:@"name"];
    if(content)
       [studName addObject:content];

}

}


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


    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...
cell.textLabel.text = [studName objectAtIndex:indexPath.row];
return cell;

}

我不确定 NSMutableArray。

//Json2ViewController.h

@interface Json2ViewController : UIViewController {
NSArray * list;
NSString * content;
NSMutableArray *studName;

}

4

1 回答 1

1

试试这个:

 //Declare NSMutableArray *studName= [[NSMutableArray alloc] init]; in your .h file.
// 3. iterate the array; each element is a dictionary...


 for (NSDictionary *results in list)
    {
        // 3 ...that contains a string for the key "stunde"
        content = [results objectForKey:@"n_name"];
        if(content)
            [studName addObject:content];
    }  

并在索引处的行的表委托方法单元格中

  // Configure the cell...
            cell.textLabel.text = [studName objectAtIndex:indexPath.row];
于 2013-01-06T12:26:46.633 回答