I am using Xcode creating an iOS app. I am also using a SQL server (xampp). It works good overall. But I have problem at the following point:
-I have view controller with a Table View and I am populating this table using the result of the SQL query (imagine taglist is an array containing 12 rows)
- (void)viewDidLoad
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"listtags", @"command", nil] onCompletion:^(NSDictionary *json) {
NSArray* result = [[NSArray alloc] initWithArray:[json objectForKey:@"result"]];
taglist =[[NSMutableArray alloc] initWithArray:result];
[taglist removeAllObjects];
for(NSDictionary* i in result){
[taglist addObject:[i objectForKey:@"tag"]];
}
NSLog(@"sql %u",taglist.count);
}];
NSLog(@"did load %u",taglist.count);
}
And there are two required methods for tableviews
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"table %u",taglist.count);
return taglist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"cell %u",taglist.count);
cell.textLabel.text = [taglist objectAtIndex:indexPath.row];
return cell;
}
The problem is since it takes a while to get the query result, my table methods use the empty taglist array to populate. The output of the NSLogs are like this
2012-12-19 14:35:41.470 iReporter[33507:c07] did load 0
2012-12-19 14:35:41.473 iReporter[33507:c07] table 0
2012-12-19 14:35:41.476 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.479 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.481 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.483 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.484 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.486 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.487 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.489 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.493 iReporter[33507:c07] cell 0
2012-12-19 14:35:41.564 iReporter[33507:c07] sql 12
Which means the taglist array is empty after the viewDidload , numberofRows and cellForRow functions. But when the query is completed I can get the correct value. But by then my table properties are already defined so it is no use,
My question is , is there a way to wait for that response and after making sure that it is not empty then to use it to create the table.
Thank you
After you get the results from your query and you update
taglist, call[self.tableView reloadData].