2011-01-07 138 views
1

我试图创建一个tableview的表头视图内的一个tableview。我想用searchDisplayController来管理一切。 我已经创建了一切以编程方式(我对IB感到不舒服)试图设置所有正确的属性,但似乎我错过了一些东西,因为当表出现时我无法编辑文本在搜索栏中看到任何动画。 下面是代码的一部分:SearchBar与searchDisplayController以编程方式编辑

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UISearchBar *searchBarTMP=[[UISearchBar alloc]init]; 
    self.searchBar=searchBarTMP; 
    [searchBarTMP release]; 
    self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone; 
    self.searchBar.delegate=self; 
    self.searchBar.showsScopeBar=YES; 
    self.searchBar.keyboardType=UIKeyboardTypeDefault; 
    self.searchBar.userInteractionEnabled=YES; 
    self.searchBar.multipleTouchEnabled=YES; 

    self.searchBar.scopeButtonTitles=[NSArray arrayWithObjects:NSLocalizedString(@"City",@"Scope City"),NSLocalizedString(@"Postal Code",@"Scope PostalCode"),nil]; 
    self.tableView.tableHeaderView=searchBar; 
    self.searchBar.selectedScopeButtonIndex=0; 
    self.navigationItem.title=NSLocalizedString(@"Store",@"Table title"); 

    //SearchDisplayController creation 
    UISearchDisplayController *searchDisplayControllerTMP = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self]; 
    self.searchDisplayController=searchDisplayControllerTMP; 
    [searchDisplayControllerTMP release]; 
    self.searchDisplayController.delegate=self; 
    self.searchDisplayController.searchResultsDelegate=self; 
    self.searchDisplayController.searchResultsDataSource=self; 

    //....continue 
} 

我知道,当你独自一人使用搜索栏,你必须处理其委托协议,但我猜的searchDisplayController为您管理的苹果样品中看到码。 (与IB建立起来)。

有什么建议吗? 谢谢 安德烈

回答

1

发现... 投入表视图的头之后必须写

[self.searchBar sizeToFit]; 
0

如果使用ARC,请确保您的UISearchDisplayController在创建伊娃你的头文件。

,如果您使用的UISearchDisplayController:

UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];

它将被ARC得到释放,也不会调用任何委托方法,当你调用self.searchDisplayController(该UIViewController的属性)这将是nil

所以,解决方法是: 在你的头文件(.h):

@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> { 
     UISearchDisplayController* searchDisplayController; 
     UISearchBar *searchField; 
     UITableView* tableView; 
     NSArray* searchResults; 
} 

,并在实现(.M)文件:

searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)]; 
searchField.delegate = self; 

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self]; 
searchDisplayController.delegate = self; 
searchDisplayController.searchResultsDataSource = self; 
searchDisplayController.searchResultsDelegate = self; 

tableView.tableHeaderView = searchField; 
tableView.contentOffset = CGPointMake(0, searchField.frame.size.height); 

当这样实现的,你可以在代码的其余部分调用self.searchDisplayControllersearchDisplayController