2

請原諒我對這個問題的無知,但我仍然試圖掌握基本知識,並且現在一直在我的頭上撞牆。以編程方式將UISearchBar和UISearchDisplayController添加到UITableView

我基本上有一個NSArray,我用它來填充UITableView,我希望用戶能夠搜索 - 以及在用戶類型中篩選包含搜索條件中字符的結果。

有相當多的教程了那裏掛鉤UITableViewsUISearchDisplayControllers,但它們都使用了XIB文件,我想添加一切編程

我有UITableView,與在UIView所示的內容時,用戶點擊一個按鈕,我出現在相同視圖的頂部添加了UISearchBar,和I「認爲」我添加了一個UISearchDisplayController。但是,我似乎無法讓所有三人都打得很好。

這裏是我到目前爲止

- (IBAction)createSearchList:(id)sender { 

//Make the list 
AAPjsonParse * makeTheFullList = [[AAPjsonParse alloc] init];  
fullListOfRecipesForTableView = [makeTheFullList generateFullListOfRecipes]; 
fullListForSearchResultsInTableView = fullListOfRecipesForTableView; 
recipeListForTable = [fullListForSearchResultsInTableView allKeys]; 
recipeListForTable = [recipeListForTable sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 


//Create the UITableView 
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 300, 724) style:UITableViewStylePlain]; 
tableView.dataSource = self; 
tableView.delegate = self; 

//Add the TableView to the view 
[self.searchView addSubview:tableView]; 

//Create a UISearchBar for the TableView 

UISearchBar * searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)]; 
//Set the searchBar as the delegate 
searchBar.delegate = self; 
//Add the searchBar to the UITableView 
[self.searchView addSubview:searchBar]; 

//Create the searchViewController 
UISearchDisplayController * searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
//Set the dataSource for the search as self 
searchController.searchResultsDataSource = self; 
//Set the results delegate as self – this is the view that's overlaid onto the full listing view (I think?) 
searchController.delegate = self; 

} 

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return [recipeListForTable count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString * CellIdentifier = @"RecipeCell"; 

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

NSString * cellTitle = [recipeListForTable objectAtIndex:indexPath.row]; 

cell.textLabel.text = cellTitle; 

return cell; 
} 

我也得到了一些東西在我的實現文件的頂部來存儲各種位

{  
NSArray * recipeListForTable; 
NSMutableArray * recipeListForSearch; 
int selectedRecipeIndex; 

NSDictionary * fullListOfRecipesForTableView; 
NSMutableDictionary * fullListForSearchResultsInTableView; 

UITableView * tableView; 
} 
+0

可能重複的[如何使用UISearchBar和UISearchDisplayController](http://stackoverflow.com/questions/18719517/how-do-i-use-the-uisearchbar-and-uisearchdisplaycontroller) – fejese

回答

1

此代碼將一個搜索欄添加到您的tableview

searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tblView.frame), 44)]; 
     searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; 
     searchBar.autocorrectionType = UITextAutocorrectionTypeNo; 
     // searchBar.tintColor = [UIColor lightGrayColor]; 



    searchBar.placeholder = @"What are you eating today..."; 
    searchBar.delegate = self; 
    searchBar.searchBarStyle = UISearchBarStyleMinimal; 

    searchDisplayController.delegate = self; 
    searchDisplayController.searchResultsDataSource = self; 

     [self.tblView setTableHeaderView:searchBar]; 

添加搜索欄後,檢查如何傳遞數據並設置代表數據源, 我建議你通過這個Tutorial

相關問題