0

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

3 回答 3

1

为什么不在收到对象时添加它们?由于此代码块将异步执行,您可以准备数组,设置表格视图,然后执行填充数组并刷新表格视图的块。

像这样的东西:

// 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];
        });
    }
});
于 2013-08-05T07:37:57.503 回答
0

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

   dispatch_sync(dispatch_get_main_queue(), ^{
     array = myJsonArray; 
     [self.tableView reloadData];
于 2013-08-05T09:02:22.527 回答
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.
于 2013-08-05T07:44:26.137 回答