【问题标题】:use of UISearchController but tableView is not reloaded使用 UISearchController 但未重新加载 tableView
【发布时间】:2016-02-14 09:21:23
【问题描述】:

我正在尝试添加搜索栏来搜索我的 tableView。只有一个 TableViewController 添加了 searchBar。我检查了所有现有指南和苹果示例代码并按照说明进行操作,但不知何故我的 tableView 在输入搜索字符串后没有重新加载。我用 NSLog 检查了我的代码,我确定搜索文本已正确上传,并且使用 NSPredicate 过滤了新数组。我在“updateSearchResultsForSearchController:”方法的末尾添加了 [self.tableView reloadData],但仍然是我的tableViewController 未更新。我是 iOS 编程新手,不知道我做错了什么。感谢您是否可以快速浏览我的代码并向我提供反馈。总结如下:

@property (nonatomic,strong) UISearchController *searchController;
@property (nonatomic,strong) NSArray *myList;   //original data list
@property (nonatomic,strong) NSArray *mysearchResult;   //filtered list



  - (void)viewDidLoad {
        [super viewDidLoad];
        self.searchController=[[UISearchController alloc]initWithSearchResultsController:nil];
        self.searchController.searchResultsUpdater=self;
        self.searchController.dimsBackgroundDuringPresentation=NO;
        self.searchController.searchBar.delegate=self;
        self.tableView.tableHeaderView=self.searchController.searchBar;
        self.definesPresentationContext=YES;
        [self.searchController.searchBar sizeToFit];
    }
    -(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString=self.searchController.searchBar.text;
    NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
    self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
    [self.tableView reloadData];
}

tableView代码的“CellForRowAtIndexPath”方法:(类似“numberOfRowsInSection”更新)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes..... 
if (tableView==self.tableView) {
        cell.cellLabel.text=[self.myList objectAtIndex:indexPath.row];
    } else 
        cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];

    return cell;
}

【问题讨论】:

  • 你分配了tabeView的dataSource和delegate吗?
  • 你只有一个TableView吗?如果是这样,您的 if 条件将始终为真。并且您的数据总是从 self.myList 加载。解决方案如下。

标签: objective-c ios9 reloaddata uisearchcontroller


【解决方案1】:

第一次在填充 tableView 之前使用mysearchResult而不是myList。此时两个数组中的数据应该相同。

self.mysearchResult = [[NSArray alloc] initWithArray:myList];

现在使用谓词后,您正在更新您的mysearchResult,这很好。当您再次取消搜索时,从实际上包含所有原始数据的 myList 填充您的 mysearchResult

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    [self.mysearchResult count];
}
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomCell codes..... 

//Remove your If condition from here
        cell.cellLabel.text=[self.mysearchResult objectAtIndex:indexPath.row];
    return cell;
}

【讨论】:

  • 谢谢,现在正在显示搜索结果。目前,当我单击搜索栏时,它会在使用搜索结果填充表格之前隐藏数据,我将在未来几天尝试修复它。有什么想法吗?
  • 请在搜索栏中验证委托方法。可能是您的数组在该方法中变空,并且您当时正在重新加载数据。其次将我的答案标记为已接受;)我解决了您的问题:)
  • 即使在取消搜索后,表格也不会重新加载,我想我会创建另一个类似于苹果示例代码的 tableViewcontroller。我想投票,但由于我是新人,它说我需要先赢得声誉。对吗?
  • Search Controller 希望对您有所帮助。
  • 谢谢。顺便说一句,我接受了答案,之前点击了错误的答案:)
【解决方案2】:

试试这个,可能有用:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSString *searchString=self.searchController.searchBar.text;
    NSPredicate *mypredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchString];
    self.mysearchResult=[self.myList filteredArrayUsingPredicate:mypredicate];
    self.searchBar.scopeButtonTitles = self.mysearchResult;  
}

【讨论】:

  • 我尝试了下面的 Shehzad Ali 解决方案,效果很好,但感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多