Iphone 如何在自定义tableView单元格中显示JSON值数组

Iphone 如何在自定义tableView单元格中显示JSON值数组,iphone,objective-c,Iphone,Objective C,我想将NSLog中获取的(myJsonArray中的NSDictionary*jsonDictionary)的值传递到[array addObject:[[SaveList alloc]initWithEmail:email withPhone:phone withDate:date withName:name] 代码在这里 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)

我想将NSLog中获取的
(myJsonArray中的NSDictionary*jsonDictionary)
的值传递到
[array addObject:[[SaveList alloc]initWithEmail:email withPhone:phone withDate:date withName:name]

代码在这里

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    NSError *error = nil;
    NSURL *url = [NSURL URLWithString:@" http:// Some url "];

    NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
    NSLog(@"\n\n JSON : %@, \n Error: %@", json, error);

    if(!error)
    {
        NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
        NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
       // NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
        for(NSDictionary *jsonDictionary in myJsonArray)
        {
            NSLog(@"JSON Dictionary = %@", jsonDictionary);

            NSString *name = jsonDictionary[@"Name"];
            NSString *date = jsonDictionary[@"Date"];
            NSString *email = jsonDictionary[@"Email"];
            NSString *phone = jsonDictionary[@"Phone"];
            NSLog(@"Name = %@", name);
            NSLog(@"Date = %@", date);
            NSLog(@"Email = %@", email);
            NSLog(@"Phone = %@", phone);
        }

    }
});



//Table implementation
array = [[NSMutableArray alloc]init];

//**Get email, phone, date, name here**
    [array addObject:[[SaveList alloc] initWithEmail:email withPhone:phone withDate:date withName:name]];

self.tableView.dataSource = self;
self.tableView.delegate = self;

为什么不在接收对象时添加它们?由于这段代码将异步执行,因此您可以准备数组,设置tableview,然后执行填充数组并刷新tableview的块

大概是这样的:

// Prepare your array
array = [NSMutableArray arrayWithCapacity:0];

// Set your tableview's datasource & delegate
self.tableView.dataSource = self;
self.tableView.delegate = self;

// Fetch data asynchronously
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    NSError *error = nil;
    NSURL *url = [NSURL URLWithString:@" http:// Some url "];

    NSString *json = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:&error];
    NSLog(@"\n\n JSON : %@, \n Error: %@", json, error);

    if(!error)
    {
        NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
        NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
        NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:myJsonArray.count];

        for(NSDictionary *jsonDictionary in myJsonArray)
        {

            NSString *name = jsonDictionary[@"Name"];
            NSString *date = jsonDictionary[@"Date"];
            NSString *email = jsonDictionary[@"Email"];
            NSString *phone = jsonDictionary[@"Phone"];


            //**Get email, phone, date, name here**
            [tmp addObject:[[SaveList alloc] initWithEmail:email 
                                                   withPhone:phone 
                                                    withDate:date 
                                                    withName:name]];
        }

        // Reload your tableview
        dispatch_sync(dispatch_get_main_queue(), ^{
            array = tmp; // Or add them to your datasource array, whatever suits you...
            [self.tableView reloadData];
        });
    }
});

//每当在表视图中添加新对象时,都需要重新加载它。它确实像线一样工作

   dispatch_sync(dispatch_get_main_queue(), ^{
     array = myJsonArray; 
     [self.tableView reloadData];

你能澄清一下这个问题吗?您的意思是想知道如何将数组中的数据添加到tableview?请注意,也不应在后台线程上修改tableview数据源数组。你必须先进入一个单独的数组。-也许你是这个意思,但从你的示例代码中看不太明显。@MartinR当然你是对的。感谢您指出out.UITableViewCell已被处理。如果我给@“一些默认字符串”,它会工作。现在我需要将for循环中的json值传递给[array addObject:…],然后在将对象添加到数组后,需要重新加载表视图;
   dispatch_sync(dispatch_get_main_queue(), ^{
     array = myJsonArray; 
     [self.tableView reloadData];