请原谅我对这个问题的无知,但我仍然试图掌握基本知识,并且现在已经把头撞到墙上几天了。
我基本上有一个NSArray
我用来填充我希望用户能够搜索的UITableView
- 并且过滤包含搜索条件中字符的结果,作为用户类型。
有很多教程用于连接UITableViews
和UISearchDisplayControllers
,但它们都使用xib文件,我想以编程方式添加所有内容。
我有UITableView
,当用户点击按钮时,其内容显示在UIView
中,我似乎已将UISearchBar
添加到同一视图的顶部,并且我想'我添加了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 :(得分: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。