2014-03-12 94 views
0

我无法搜索,因为我的搜索栏是tableView的头,然后我重新加载tableView我没有搜索结果。Searchbar在tableView和重新加载数据

我知道有很多方法可以解决它,但更明智的是什么?

我的代码

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)sectionIndex 
{ 
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, 270, kRowHeight)]; 
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 270, kRowHeight)]; 
self.searchBar.delegate = self; 
view.opaque = NO; 
view.backgroundColor = [UIColor clearColor]; 
self.searchBar.opaque = NO; 
self.searchBar.translucent = NO; 
self.searchBar.backgroundColor = [UIColor clearColor]; 
[view addSubview: self.searchBar]; 
self.searchBar.barStyle = UISearchBarStyleDefault; 
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin ; 
self.searchBar.barTintColor = [UIColor clearColor]; 

return view; 

}

而且我要的是后来我滚动的tableView我的搜索栏将在顶部消失,然后我向上滚动,它就会出现。做任何简单的事情吗?

+0

为什么不使用UITableViewController? –

+0

@Grzegorz Krukowski,你是什么意思? – user3358463

+0

为什么不把搜索栏放在tableview之外? – Merlevede

回答

0

您需要使用tableHeaderView而不是节标头。只是放在你的viewDidLoad

UISearchBar* searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 270, kRowHeight)]; 
self.tableView.tableHeaderView = searchBar; 
相关问题