I am trying to get the ‘Search Bar and Display Controller’ functionality to work in an iOS app. I am able to NSLog in repsonse to a search query and hardcode in a new array but am unable to get the table view to repopulate. I have the following for in response to a user submit on the search button:
- (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];
}
Any ideas on making this repopulate would be appreciated.
thx
edit #1
It looks like this is populating the _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];
}
Should I just create a new _objects and use insertNewObject rather than the addObject code I have above? Would this bypass the need to deal with the dataSource property of the table view?
edit 2
per @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
I have a UITableView that uses an NSMutableArray to hold the data. Here’s how it works: set the UISearchDisplayController’s delegate to your TableView controller, and when the UITableViewDelegate methods are called (numberOfRows, numberOfSections, cellForRowAtIndexPath, etc) you can do the following to serve up the search data when appropriate:
You should take a look at the UISearchDisplayDelegate documentation. You can use these methods to update your filteredListContent array, as follows: