2014-03-12 89 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; 
相關問題