ios - 以编程方式将搜索栏和 UISearchDisplayController 添加到 UITableView

标签 ios objective-c uitableview uisearchbar uisearchdisplaycontroller

请原谅我对这个问题的无知,但我仍在努力掌握基础知识并且现在已经用头撞墙几天了。

我基本上有一个 NSArray,我用它来填充一个 UITableView,我希望用户能够通过它进行搜索 – 并过滤包含的结果搜索条件中的字符,作为用户键入。

有很多教程用于连接 UITableViewsUISearchDisplayControllers,但它们都使用 xib 文件,我想以编程方式添加所有内容.

我有 UITableView,当用户点击按钮时,它的内容显示在 UIView 中,我似乎添加了 UISearchBar在同一 View 的顶部,我“认为”添加了一个 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;
}

最佳答案

此代码将向您的 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];

添加搜索栏后检查如何传递数据并设置委托(delegate)数据源, 我建议你通过这个 Tutorial .

关于ios - 以编程方式将搜索栏和 UISearchDisplayController 添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19110925/

相关文章:

ios - 从模态视图返回时,向已打开的 View 写入一些内容

iOS:在 alloc 和 init 之后返回 *nil description* 的对象

iphone - 对于 Iphone,如何从当前加载的 View 引用 Root View Controller ,以便可以替换/切换 View

iphone - tableView:commitEditingStyle:forRowAtIndexPath:导致断言失败

swift - 删除 viewController 的边距

iphone - PhoneGap/iOS - 如何让 Childbrowser 忽略指向 Appstore 的链接?

objective-c - 自动引用计数和 UINavigationController

xcode - 使用最初隐藏在 Swift iOS 9 中的 TableView 实现 UISearchController

iphone - 一周后根据第一个字段中输入的日期自动显示第二个字段中的日期

ios - 将 CoreData 数据 blob 移动到单独的对象中