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

时间:2013-08-05 07:13:11

标签: iphone objective-c

我想将我在NSLog中获得的(NSDictionary *jsonDictionary in myJsonArray)内的值传递给[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;

3 个答案:

答案 0 :(得分:1)

为什么不在收到对象时添加它们?由于这段代码将以异步方式执行,您可以准备数组,设置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];
        });
    }
});

答案 1 :(得分:0)

set number of rows 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
[array count];
}
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    SavedList *savedList = [array objectAtIndex:indexPath.row];
    cell.text = savedList.name;

    }
    return cell;
    }

    I hope it will be helpful.

答案 2 :(得分:0)

//无论何时在其中添加新对象,都需要重新加载表视图数据。它确实像一个线程一样工作。

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