试图让“搜索栏和显示控制器”重新填充表格视图

时间:2012-03-20 18:05:04

标签: ios ios5

我正在努力让“搜索栏和显示控制器”功能在iOS应用中运行。我能够在一个新的数组中对一个搜索查询和硬编码进行回复,但是我无法让表视图重新填充。为响应用户在搜索按钮上的提交,我有以下内容:

- (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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我有一个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;
}