尝试获取'Search Bar and Display Controller'以重新填充表视图



我正在尝试让"搜索栏和显示控制器"功能在iOS应用程序中工作。我能够NSLog响应搜索查询并在新阵列中硬编码,但无法获得要重新填充的表视图。我有以下用于响应用户在搜索按钮上提交:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
  NSLog(@"You clicked the search bar");
  NSMutableArray *filteredResults=[[NSMutableArray alloc] init];
  Person *filteredPerson=[[Person alloc] init];
  filteredPerson.firstName=@"Josie - here i am";
  [filteredResults addObject:filteredPerson];
  _objects=filteredResults;
  self.tableView.dataSource = _objects;
  [self.tableView reloadData];
}

任何关于重新填充的想法都将不胜感激。

thx

编辑#1看起来这是在填充_objects NSMutableArray:

- (void)insertNewObject:(id)sender
{
    if (!_objects) {
        _objects = [[NSMutableArray alloc] init];
    }
    [_objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

我应该只创建一个新的_objects并使用insertNewObject而不是上面的addObject代码吗?这会绕过处理表视图的dataSource属性的需要吗?

编辑2根据@ian

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    /*
    NSDate *object = [_objects objectAtIndex:indexPath.row];
    cell.textLabel.text = [object description];
    */
    Person *rowPerson = [_objects objectAtIndex:indexPath.row];
    cell.textLabel.text = [rowPerson firstName];
    return cell;
}

thx

我有一个UITableView,它使用NSMutableArray来保存数据。它的工作原理如下:将UISearchDisplayController的委托设置为TableView控制器,当调用UITableViewDelegate方法(numberOfRows、numberOfSections、cellForRowAtIndexPath等)时,您可以在适当的时候执行以下操作来提供搜索数据:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numberOfRows = 0;
    if (tableView == self.searchDisplayController.searchResultsTableView)
    {
        //This is your search table --- use the filteredListContent (this is the NSMutableArray)
        numberOfRows = [filteredListContent count];
    }
    else
    {
        //Serve up data for your regular UITableView here.
    }
    return numberOfRows;
}

您应该查看UISearchDisplayDelegate文档。您可以使用这些方法更新filteredListContent数组,如下所示:

#pragma mark -
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText
{   
    //In this method, you'll want to update your filteredListContent array using the string of text that the user has typed in. For example, you could do something like this (it all depends on how you're storing and retrieving the data):
    NSPredicate *notePredicate = [NSPredicate predicateWithFormat:@"text contains[cd] %@", searchText];
    self.filteredListContent = [[mainDataArray filteredArrayUsingPredicate:notePredicate] mutableCopy];
    }

#pragma mark UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString];
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

self.tableView.dataSource = _objects;将您的数组设置为UITableView的数据源。我假设您没有实现UITableViewDataSource协议的NSArray子类?

请尝试删除该行,并让现有的数据源处理程序处理数据中的更改。

最新更新