iPhone UI搜索栏视图未立即更新?
我目前在我的RootViewController中有一个UISearchBar和UIDisplayController定义为:iPhone UI搜索栏视图未立即更新?,iphone,objective-c,uisearchbar,uisearchdisplaycontroller,Iphone,Objective C,Uisearchbar,Uisearchdisplaycontroller,我目前在我的RootViewController中有一个UISearchBar和UIDisplayController定义为: - (void)viewDidLoad { [super viewDidLoad]; //Add the search bar aSearchBar = [[UISearchBar alloc] initWithFrame:CGRectZero]; [aSearchBar sizeToFit]; aSearchBar.deleg
- (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方法?关联表视图的代码可能会有所帮助,但我猜您没有调用
[self.tableView reloadData]
搜索完成后发现解决方案是将searchDisplayController委托和数据源指向它正在实现的表视图:
searchDC.delegate = self;
searchDC.searchResultsDataSource = self.tableView.dataSource;
searchDC.searchResultsDelegate = self.tableView.delegate;
是否有一个特定的方法我应该调用它?可能在搜索完成后,我当前有一个“观察者”检测数组对象何时更改以更新tableView。有没有一种方法可以通过编程触发“取消按钮”事件?