我正在使用Xcode创建一个iOS应用程序。我也在使用SQL Server(xampp)。总体来说效果不错。但是我在以下几点有问题:

-我有一个带有表视图的视图控制器,并且正在使用SQL查询的结果填充该表(想象中的标签列表是一个包含12行的数组)

- (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);

}

表格视图有两种必需的方法
- (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;

}

问题是由于要花一些时间才能获得查询结果,因此我的表格方法使用空的标记列表数组进行填充。 NSLogs的输出是这样的
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

这意味着在viewDidload,numberofRows和cellForRow函数之后,标签列表数组为空。但是查询完成后,我可以获得正确的值。但是到那时我的表属性已经定义好了,所以没有用,
我的问题是,有没有办法等待该响应,并确保它不为空之后再使用它来创建表。
谢谢

最佳答案

从查询中获得结果并更新taglist后,请调用[self.tableView reloadData]

07-24 09:37
查看更多