3

我现在有一个UISearchBar和UIDisplayController在我的RootViewController的定义为:iPhone UISearchBar视图不立即更新?

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    //Add the search bar 
    aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; 
    [aSearchBar sizeToFit]; 
    aSearchBar.delegate = self; 
    aSearchBar.placeholder = @"Search YouTube..."; 

    self.tableView.tableHeaderView = aSearchBar; 

    searchDC = [[UISearchDisplayController alloc] initWithSearchBar:aSearchBar contentsController:self]; 

    [self performSelector:@selector(setSearchDisplayController:) withObject:searchDC]; 

    searchDC.delegate = self; 
    searchDC.searchResultsDataSource = self; 
    searchDC.searchResultsDelegate = self; 

    [aSearchBar release]; 
    [searchDC release]; 

} 

当搜索按钮被触发时,执行该事件来运行API调用:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { 
    [videoList removeAllObjects]; 
    if (searchBar.text.length > 0) { 
     NSString *searchCriteria = [searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 

     YTJAppDelegate *appDelegate=(YTJAppDelegate*)[[UIApplication sharedApplication] delegate]; 
     [appDelegate searchWithCriteria:searchCriteria]; 

    }  
} 

的数据被取出正确。然而。只有当我点击搜索上的“取消”按钮时,它才会变得可见。

如何在数据源更新/搜索按钮被点击时正确更新视图?我需要实现一个特殊的SearchDelegate方法吗?

回答

1

关联表视图的代码可能会有所帮助,但我会猜测,搜索完成后

+0

是否有一个特定的方法,我应该调用它? – unicornherder 2010-09-03 20:43:16

+0

可能在搜索完成后 – 2010-09-03 20:44:04

+0

当Array对象更改为更新tableView时,我当前有一个“观察者”检测。 有没有一种方法来编程激发'取消按钮'事件? – unicornherder 2010-09-03 20:57:09

1

原来的解决方案是指向searchDisplayController代表和数据源中的你是不是叫[self.tableView reloadData]它正在执行的表格视图:

searchDC.delegate = self; 
searchDC.searchResultsDataSource = self.tableView.dataSource; 
searchDC.searchResultsDelegate = self.tableView.delegate;