• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 15:00:16 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

请原谅我对这个问题的无知,但我仍在努力掌握基础知识,并且已经为此苦苦挣扎了几天。

我基本上有一个 NSArray 我用来填充一个 UITableView 我希望用户能够搜索 - 并过滤包含的结果用户键入时搜索条件中的字符。

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

我有 UITableView,当用户点击按钮时,其内容显示在 UIView 中,我似乎添加了 UISearchBar在同一个 View 的顶部,我“认为”我添加了一个 UISearchDisplayController。但是,我似乎无法让这三个人都玩得很好。

这是我目前所拥有的

- (IBAction)createSearchListid)sender {

//Make the list
AAPjsonParse * makeTheFullList = [[AAPjsonParse alloc] init];    
fullListOfRecipesForTableView = [makeTheFullList generateFullListOfRecipes];
fullListForSearchResultsInTableView = fullListOfRecipesForTableView;
recipeListForTable = [fullListForSearchResultsInTableView allKeys];
recipeListForTable = [recipeListForTable sortedArrayUsingSelectorselector(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) numberOfSectionsInTableViewUITableView *)tableView {
return 1;
}

-(NSInteger) tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
return [recipeListForTable count];
}

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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;
}



Best Answer-推荐答案


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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap