【问题标题】:display search result in table view in objective-c在objective-c的表视图中显示搜索结果
【发布时间】:2015-11-18 10:09:33
【问题描述】:
  1. 我在视图控制器中创建了一个带有搜索栏的表格视图,但表格视图没有显示搜索结果。
  2. 这就是我的代码的工作方式。首先我创建一个数组来保存数据>>在表格视图中显示数组数据>>使用搜索栏过滤表格视图。

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    self.inventoryarray =[[NSArray alloc] initWithObjects: @"apple",@"samsung",@"HTC",@"LG",@"Sony",@"Motorola",@"Nexus",@"Asus" ,nil];
    self.searchresult =[[NSArray alloc]init];    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma table View methods

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

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

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

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

#pragma search methods

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


-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString  {    
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]] ;
    return YES;
}

【问题讨论】:

    标签: ios objective-c uitableview uisearchbar


    【解决方案1】:

    你忘了refeshtable上的数据

     -(void) filterContentForSearchText:(NSString *)searchText scope:   (NSString *)scope
    {
        NSPredicate *resultPredicate = [NSPredicate
                                predicateWithFormat:@"SELF contains[cd] %@",
                                searchText];
    
        self.searchresult = [self.inventoryarray filteredArrayUsingPredicate:resultPredicate];
    
        // you forget to reload Data
        [self.searchDisplayController.searchResultsTableView reloadData];
        // else use 
    
        [yourtableView reloadData];
    }
    

    建议:UISearchDisplayController 已弃用,请继续使用UISearchController,用于tutorial

    【讨论】:

      【解决方案2】:

      您只需要在filterContentForSearchText 方法的最后一个[tableView reloadData];

      如果你不想使用 UISearchController 那么... - 'UISearchDisplayController' 已弃用:在 iOS 8.0 中首次弃用 - UISearchDisplayController 已替换为 UISearchController

      如果您不想使用UISearchController,请使用以下方法:

      - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {} 并重新加载 tableview。

      【讨论】:

        猜你喜欢
        • 2011-06-24
        • 1970-01-01
        • 2021-09-24
        • 2013-01-15
        • 1970-01-01
        • 2015-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多