Ios 使用UISearchBar Filterable TableView

Ios 使用UISearchBar Filterable TableView,ios,iphone,objective-c,uitableview,uisearchbar,Ios,Iphone,Objective C,Uitableview,Uisearchbar,我对objective c非常陌生,希望这一切都有意义。我在YouTube上学习了这个教程。这个程序不起作用。 你可以从这里下载这个项目。指导我 要使此程序正常运行。使用以下教程是获得答案和github中示例代码的简单方法,链接为(void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar;//文本开始编辑时调用 { 如果(搜索) { 搜索=是; LetsUserElectrow=是; } 其他的 { 搜索=否; LetsUserElect

我对objective c非常陌生,希望这一切都有意义。我在YouTube上学习了这个教程。这个程序不起作用。 你可以从这里下载这个项目。指导我
要使此程序正常运行。

使用以下教程是获得答案和github中示例代码的简单方法,链接为(void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar;//文本开始编辑时调用 { 如果(搜索) { 搜索=是; LetsUserElectrow=是; } 其他的 { 搜索=否; LetsUserElectrow=是; } //self.tbl_searchtable.scrollEnabled=否; } -(无效)搜索栏取消按钮选中:(UISearchBar*)搜索栏 { 搜索=否; LetsUserElectrow=是; searchBar.text=@; [搜索栏辞职FirstResponder]; [self.tbl_searchuser reloadData]; } -(NSIndexPath*)tableView:(UITableView*)tableView将选择Rowatindexpath:(NSIndexPath*)indexPath { 如果(Letsuserseleow) 返回索引XPath; 其他的 返回零; } -(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本;//文本更改时调用(包括清除) { _tbl_searchuser.hidden=FALSE; [CopyListofItemRemoveAllObjects]; 如果([searchText length]>0) { 搜索=是; LetsUserElectrow=是; self.tbl_searchuser.scrollEnabled=是; [自搜索表视图]; } 其他的 { 搜索=否; Letsuserseleow=否; [_searchbar辞职FirstResponder]; //self.tbl_searchtable.scrollEnabled=否; } [项目保留副本列表]; [self.tbl_searchuser reloadData]; } -(无效)searchBarSearchButtonClicked:(UISearchBar*)搜索栏;//按下键盘搜索按钮时调用 { [自我隐藏]; //[自搜索表视图]; } -(无效)searchtableview { _tbl_searchuser.frame=CGRectMake(0,65,320,140); NSString*searchText=\u searchbar.text; NSLog(@“%@”,阵列用户); 对于(int l=0;l<[array_allusers count];l++) { NSString*tempstr=[[array_allusers objectAtIndex:l]objectForKey:@“username”]; NSRange rngstr=[tempstr rangeOfString:searchText选项:(NSAnchoredSearch | NSCaseInsensitiveSearch)]; 如果(rngstr.length>0) { [copylistofitem addObject:[array_allusers objectAtIndex:l]; [项目保留副本列表]; } } NSLog(@“%@”,copylistofitem); NSSortDescriptor*排序=[NSSortDescriptor SortDescriptor WithKey:@“用户名”升序:是]; [copylistofitem sortUsingDescriptors:[NSArray arrayWithObject:sort]]; [_tbl_searchuser reloadData]; } -(无效)隐藏 { [_searchbar辞职FirstResponder]; _tbl_searchuser.frame=CGRectMake(0,65320274); } -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节 { 如果(搜索和[_searchbar.text length]>0) 返回[copylistofitem count]; 其他的 返回数组; } NSMutableArray*copylistofitem; 布尔搜索; 布勒·莱瑟塞洛;
在视图控制器.h文件中

进口
最后在视图顶部添加一个搜索栏显示控制器

u要使用uitableviewyes在搜索栏中搜索内容。.我要在UITableViews中搜索搜索栏中的内容这不是一个平均值,您的意思只是视图控制器,只需自定义编码,如果您选择了did select行中的单元格,那时候你需要分配视图控制器并推送对象,它很简单如果将本教程转换为xib很简单请上传它只是为了使用uitableview在搜索栏中搜索内容的功能。因为我只是初始学习者。嗯,k,等等,有任何疑问,回复o在视图控制器上粘贴代码显示tableview…当我添加搜索栏时…我设置了与文件所有者连接的出口代理…并将出口视图引用为文件所有者..它不显示搜索栏..只添加搜索栏和显示控制器,不提供出口,如果我只是简单地将搜索栏添加到viewcontroller,它会自动检测到Bro。执行会停止,异常为:test[1170:a0b]***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[UIViewController\u loadViewFromNibNamed:bundle:]加载了“TESTBIDViewController”nib,但未设置视图出口。“***第一次抛出调用堆栈:……..u被复制粘贴我的代码,其k,只需将.h文件TESTBIDViewController替换为RecipeBookViewController
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar;                     // called when text starts editing
{
    if (searching)
    {
        searching=YES;
        letsUserSelectRow=YES;
    }
    else
    {
        searching=NO;
        letsUserSelectRow=YES;
    }
    // self.tbl_searchtable.scrollEnabled=NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar
{
    searching = NO;
    letsUserSelectRow = YES;
    searchBar.text = @"";
    [searchBar resignFirstResponder];
    [self.tbl_searchuser reloadData];
}

-(NSIndexPath *)tableView :(UITableView *)theTableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(letsUserSelectRow)
        return indexPath;
    else
        return nil;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;   // called when text changes (including clear)
{
    _tbl_searchuser.hidden=FALSE;

    [copylistofitem removeAllObjects];

    if([searchText length] > 0)
    {
        searching = YES;
        letsUserSelectRow = YES;
        self.tbl_searchuser.scrollEnabled = YES;
        [self searchtableview];
    }
    else
    {
        searching = NO;
        letsUserSelectRow= NO;
        [_searchbar resignFirstResponder];
        //self.tbl_searchtable.scrollEnabled = NO;
    }

    [copylistofitem retain];
    [self.tbl_searchuser reloadData];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar;                     // called when keyboard search button pressed
{
    [self hide];
    //[self searchtableview];
}

-(void)searchtableview
{
    _tbl_searchuser.frame=CGRectMake(0, 65, 320, 140);
    NSString *searchText = _searchbar.text;
    NSLog(@"%@",array_allusers);
    for(int l = 0; l < [array_allusers count]; l++)
    {
        NSString *tempstr = [[array_allusers objectAtIndex:l] objectForKey:@"username"];
        NSRange rngstr = [tempstr rangeOfString:searchText options:(NSAnchoredSearch | NSCaseInsensitiveSearch)];

        if(rngstr.length > 0)
        {
            [copylistofitem addObject:[array_allusers objectAtIndex:l]];
            [copylistofitem retain];
        }
    }
    NSLog(@"%@",copylistofitem);

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"username" ascending:YES];
    [copylistofitem sortUsingDescriptors:[NSArray arrayWithObject:sort]];

    [_tbl_searchuser reloadData];










}
-(void)hide
{
    [_searchbar resignFirstResponder];
    _tbl_searchuser.frame=CGRectMake(0, 65, 320, 274);

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (searching && [_searchbar.text length] > 0)
        return [copylistofitem count];
    else
        return yourarray;

}

NSMutableArray *copylistofitem;
BOOL searching;
BOOL letsUserSelectRow;
@interface RecipeBookViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
{

  NSArray *recipes;   //this is used for load the data to table
   NSArray *searchResults; //this is used for search the particular name in uitableview
}

@property (nonatomic, strong) UITableView *tableView;    //progrmatically allocationg the tableview
@synthesize tableView;  //this is used accessing the name in more than one time


- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data

searchResults=[NSArray alloc]init];

recipes = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", @"Full Breakfast", @"Hamburger", @"Green Tea", @"Thai Shrimp Cake", @"Angry Birds Cake", @"Ham and Cheese Panini", nil];

//create the tableview
tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 163, 320, 317)];
tableView.delegate=self;
tableView.dataSource=self;
[self. tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

[self.view addSubview: tableView];

}


//this is for loading the table data

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
    return [searchResults count];

} else {
    return [recipes count];

}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {
    cell.textLabel.text = [searchResults objectAtIndex:indexPath.row];
} else {
    cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
}

return cell;
}

//this is for search bar delegate method for filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate 
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];

searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}

#pragma mark - UISearchDisplayController delegate methods
 -(BOOL)searchDisplayController:(UISearchDisplayController *)controller 
 shouldReloadTableForSearchString:(NSString *)searchString
 {
 [self filterContentForSearchText:searchString 
                           scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                  objectAtIndex:   [self.searchDisplayController.searchBar
                                                 selectedScopeButtonIndex]]];

return YES;
}