我的基本问题是我似乎无法弄清楚如何将活动指示器置于顶部,这样我就可以在新数据填充UITableView时显示进程正在运行。要加载新数据,我使用的是分段控件。当按下段0时,它将加载第一个xml进给,当按下段1时,它将加载第二个xml进给。我已经尝试过使用dispatch_queue_t等。它会加载信息并重新填充表格,它只是没有显示指标。
这是我目前使用的代码。
if (Seg.selectedSegmentIndex == 0) {
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
dispatch_queue_t loader = dispatch_queue_create("a", NULL);
dispatch_async(loader, ^{
xml = [[XmlParser alloc]init];
url = [NSURL URLWithString:kTodaysReport];
dict = [[NSMutableDictionary alloc]init];
[xml loadXML:url];
[tableView addSubview:ActInd];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
});
});
}
else if (Seg.selectedSegmentIndex ==1){
// tableView = [[UITableView alloc]init];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
dispatch_queue_t loader = dispatch_queue_create("a", NULL);
dispatch_async(loader, ^{
xml = [[XmlParser alloc]init];
url = [NSURL URLWithString:kMonthsReport];
dict = [[NSMutableDictionary alloc]init];
[xml loadXML:url];
[tableView addSubview:ActInd];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
});
});
}
提前致谢。
编辑:相关代码
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_queue_t loader = dispatch_queue_create("a", NULL);
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
dispatch_async(loader, ^{
xml = [[XmlParser alloc]init];
url = [NSURL URLWithString:kTodaysReport];
dict = [[NSMutableDictionary alloc]init];
[xml loadXML:url];
[UiTableView addSubview:ActInd];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
[ActInd stopAnimating];
[UiTableView reloadData];
});
});
}
-(void)handleUpdate:(NSNotification*)notification{
mainArray = [xml parsedArray];
[UiTableView reloadData];
}
-(void)loading{
if (![xml val]) {
[ActInd startAnimating];
}else
{
[ActInd stopAnimating];
[UiTableView reloadData];
}
}
-(IBAction)segvalueChange:(id)sender{
if (Seg.selectedSegmentIndex == 0) {
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
dispatch_queue_t loader = dispatch_queue_create("a", NULL);
dispatch_async(loader, ^{
xml = [[XmlParser alloc]init];
url = [NSURL URLWithString:kTodaysReport];
dict = [[NSMutableDictionary alloc]init];
[xml loadXML:url];
[UiTableView addSubview:ActInd];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
});
});
}
else if (Seg.selectedSegmentIndex ==1){
// UiTableView = [[UITableView alloc]init];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
dispatch_queue_t loader = dispatch_queue_create("a", NULL);
dispatch_async(loader, ^{
xml = [[XmlParser alloc]init];
url = [NSURL URLWithString:kMonthsReport];
dict = [[NSMutableDictionary alloc]init];
[xml loadXML:url];
[UiTableView addSubview:ActInd];
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleUpdate:) name:@"update" object:nil];
});
});
}
else{
}
}
答案 0 :(得分:1)
问题是您在后台线程上与UI进行交互。将所有与UI相关的代码(包括与活动视图交互的代码)移动到主线程的GCD块中。