2010-06-30 50 views
4

我想要实现的是一个UISearchBar,向上移动并覆盖UINavBar,并在其右侧包含一个取消按钮。一切顺利,直到我用下面的代码行:UITableView不滚动使用UISearchBar

searchDC = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease]; 

结束意外事件发生的是刚刚的UITableView不会滚动,但一切功能正常。如果我删除该行,我的导航栏就会显示,并且搜索栏位于其下方,同样缺少取消按钮。

任何想法?

绘制的搜索栏的代码是:

self.navigationItem.title = @"Search"; 
searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.bounds.size.width, 44.0f)] autorelease]; 
searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone; 
searchBar.keyboardType = UIKeyboardTypeAlphabet; 
searchBar.delegate = self; 
[searchBar becomeFirstResponder]; 

tableView.tableHeaderView = searchBar; 
tableView.tableHeaderView.frame = CGRectMake(0.f, 0.f, 480.f, 44.f); 
searchDC = [[[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self] autorelease]; 
searchDC.searchResultsDataSource = self; 
searchDC.searchResultsDelegate = self; 
searchDC.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone; 
searchDC.searchResultsTableView.scrollEnabled = YES; 

overlayView.frame = CGRectMake(0, 50, 320, 480); 
[self.view addSubview:overlayView]; 
[self.view bringSubviewToFront:overlayView]; 

回答

0

从苹果公司的文档一个很好的例子,

Delegate for the search display controller (delegate), which responds to events such the starting or ending of a search, and the showing or hiding of the search interface.

设置delegateUITableViewController

searchDC.delegate = self; 

还要添加searchDC的searchBar到tableView的headerView

[self.tableView setTableHeaderView:searchDC.searchBar];