ios - 以编程方式将搜索栏和 UISearchDisplayController 添加到 UITableView
<p><p>请原谅我对这个问题的无知,但我仍在努力掌握基础知识,并且已经为此苦苦挣扎了几天。</p>
<p>我基本上有一个 <code>NSArray</code> 我用来填充一个 <code>UITableView</code> 我希望用户能够搜索 - 并过滤包含的结果用户键入时搜索条件中的字符。</p>
<p>有很多教程用于连接 <code>UITableViews</code> 和 <code>UISearchDisplayControllers</code> 但它们都使用 xib 文件,我想<strong>以编程方式添加所有内容</强>。</p>
<p>我有 <code>UITableView</code>,当用户点击按钮时,其内容显示在 <code>UIView</code> 中,我似乎添加了 <code>UISearchBar</code>在同一个 View 的顶部,我“<em>认为</em>”我添加了一个 <code>UISearchDisplayController</code>。但是,我似乎无法让这三个人都玩得很好。</p>
<p>这是我目前所拥有的</p>
<pre><code>- (IBAction)createSearchList:(id)sender {
//Make the list
AAPjsonParse * makeTheFullList = [ init];
fullListOfRecipesForTableView = ;
fullListForSearchResultsInTableView = fullListOfRecipesForTableView;
recipeListForTable = ;
recipeListForTable = ;
//Create the UITableView
tableView = [ initWithFrame:CGRectMake(0, 44, 300, 724) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
//Add the TableView to the view
;
//Create a UISearchBar for the TableView
UISearchBar * searchBar = [ initWithFrame:CGRectMake(0, 0, 300, 44)];
//Set the searchBar as the delegate
searchBar.delegate = self;
//Add the searchBar to the UITableView
;
//Create the searchViewController
UISearchDisplayController * searchController = [ 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 ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = @"RecipeCell";
UITableViewCell * cell = ;
if (cell == nil) {
cell = [ initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSString * cellTitle = ;
cell.textLabel.text = cellTitle;
return cell;
}
</code></pre>
<p>我的实现文件顶部还有一些东西来存储各种位</p>
<pre><code>{
NSArray * recipeListForTable;
NSMutableArray * recipeListForSearch;
int selectedRecipeIndex;
NSDictionary * fullListOfRecipesForTableView;
NSMutableDictionary * fullListForSearchResultsInTableView;
UITableView * tableView;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>此代码将向您的 <code>tableview</code></p> 添加一个搜索栏
<pre><code>searchBar = [ initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tblView.frame), 44)];
searchDisplayController = [ initWithSearchBar:searchBar contentsController:self];
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// searchBar.tintColor = ;
searchBar.placeholder = @"What are you eating today...";
searchBar.delegate = self;
searchBar.searchBarStyle = UISearchBarStyleMinimal;
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
;
</code></pre>
<p>添加搜索栏后检查如何传递数据并设置<strong>delegate</strong>和<strong>datasource</strong>,
我建议你通过这个<a href="http://www.appcoda.com/how-to-add-search-bar-uitableview/" rel="noreferrer noopener nofollow">Tutorial</a> .</p></p>
<p style="font-size: 20px;">关于ios - 以编程方式将搜索栏和 UISearchDisplayController 添加到 UITableView,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/19110925/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/19110925/
</a>
</p>
页:
[1]