无法控制回UI

时间:2012-02-12 14:18:49

标签: uitableview nsoperation nsoperationqueue

我有一个应用程序允许通过各种字段(名称,城市,ID)搜索sql db我试图将搜索作为队列中的NSOperations来实现。并且ui在第一次搜索之间更新,但由于某种原因,当操作在后台继续时,tableview将不响应触摸。不知道我错过了什么,或者我是否需要一个自定义的runloop作为操作队列,如果这样会感谢一些帮助,因为我不熟悉这里是我现有的代码

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {


    NSString *searchText = searchBar.text;
    [self.searchQueue cancelAllOperations];    //NSOperationQueue
    [self.searchResults startSearching:searchText];
    [self searchID:searchText];

    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchName:) object:searchText];
    [self.searchQueue addOperation:operation1];
    [operation1 release];

                //NSLog(@"Add city search");

    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(searchCity:) object:searchText];
    [self.searchQueue addOperation:operation2];
    [operation2 release];
}

所以会发生什么是UI保持到第一次搜索结束(没关系)并刷新表视图。当每个操作完成时,它会刷新tableview,但在此期间tableview将不会响应触摸......

任务本身看起来像这样..

-(void) searchCity:(NSString *)City{
    searchObject *myCity = [[searchObject alloc] init];
    self.searchResults.cityResults = [myCity searchCity:City];  //returns NSArray of items

    if ([self.searchResults.cityResults count] == 0) {

        [self.searchResults.cityResults addObject:[NSArray arrayWithObjects:@"Not Found.",@"",@"",@"",@"", nil]];
    }
    [self.searchResults refreshView];
        //NSLog(@"Added from City Search\n%@",self.searchResults.cityResults);
}

1 个答案:

答案 0 :(得分:0)

好的,我回答了我自己的问题,但对于下一个人......

就像确保在主线程上执行tableview刷新一样简单......

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];