UITableView无法加载JSON数据

时间:2014-05-28 07:58:38

标签: ios objective-c json uitableview

我正在开发一个应用程序,它将在UITableView中加载远程数据,但是当我运行它时,它什么也没显示。请查看并帮助我调试它。感谢

Json outPut

{
    "emp_info" =     (
                (
            1,
            firstname_one,
            lastname_one,
            "abcd@xyz",
            "500 B3 abc Town"
        ),
                (
            2,
            firstname_two,
            lastname_two,
            "abcd@xyz",
            "500 B3 abc Town"
        ),
                (
            18,
            firstname_three,
            lastname_three,
            "abcd@xyz",
            "500 B3 abc Town"
        )
    );
}

和.m文件

- (void)viewDidLoad
{
    [super viewDidLoad];

    statuses=[[NSMutableArray alloc]init];

    NSURL *myURL = [NSURL URLWithString:@"http://abcd.com/My_php/result.php"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];


    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]

                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               NSLog(@"Finished with status code: %i", [(NSHTTPURLResponse *)response statusCode]);
                               id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];


                               NSLog(@"jsonObject=%@",jsonObject);

                               statuses=[jsonObject mutableCopy];

                               [self.theTableView reloadData];
                           }];

    NSLog(@"myURL=%@",myURL);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%d",[statuses count]);
    return [statuses count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    id obj = [statuses objectAtIndex:indexPath.row];
    cell.textLabel.text =[obj valueForKey:@"Title"];

    return cell;
}

当我编译它时。它在表格中没有显示任何信息,甚至不显示表格本身。 See it shows nothing

2 个答案:

答案 0 :(得分:1)

你应该让NSArray包含来自JSON的NSDictionary的NSArrays:

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{

...

// This Array contains arrays 
   statuses=[jsonObject[@"emp_info"] mutableCopy];
   [self.theTableView reloadData];
}];

在cellForRowAtIndexPath中,从此NSArray中获取值:

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

  ...
  // this is an NSArray, not an NSDictionary:
  NSArray *obj = [statuses objectAtIndex:indexPath.row];
  cell.textLabel.text = obj[1]; // just my example
}

答案 1 :(得分:0)

您的数据是包含数组的字典。

如果只需要数组,请从json解析器返回的值对象中获取数组:

statuses=[[jsonObject valueForKey:@"emp_info"] mutableCopy];