【发布时间】:2013-10-01 07:59:54
【问题描述】:
请原谅我对这个问题的无知,但我仍在努力掌握基础知识,并且已经为此苦苦挣扎了几天。
我基本上有一个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;
}
【问题讨论】:
标签: ios objective-c uitableview uisearchbar uisearchdisplaycontroller