iPhone UISearchBar视图没有立即更新?

时间:2010-09-03 20:14:11

标签: iphone objective-c uisearchbar uisearchdisplaycontroller

我目前在我的RootViewController中定义了一个UISearchBar和UIDisplayController:

- (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方法吗?

2 个答案:

答案 0 :(得分:1)

关联表视图的代码可能会有所帮助,但我猜你在搜索完成后没有调用[self.tableView reloadData]

答案 1 :(得分:1)

事实证明解决方案是将searchDisplayController委托和数据源指向它正在实现的表视图:

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