I’m populating my tableview from an array. And this array is created by SQLite query.
So in my array, I have objects from my user-defined class.
And I use custom cells in my table. Like object.name in one layer, object.id near this layer.
So far everything’s good. But if I try to use UISearchBar, how will I re-populate my table?
This is the code I create my table.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"SpeakersCell";
SpeakersCell *cell = (SpeakersCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"SpeakersCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[SpeakersCell class]]) {
cell = (SpeakersCell *) currentObject;
break;
}
}
}
// Set up the cell
ProjectAppDelegate *appDelegate = (ProjectAppDelegate *)[[UIApplication sharedApplication] delegate];
Speaker *speakerObject = (Speaker *)[appDelegate.speakers objectAtIndex:indexPath.row];
cell.lblSpeakerName.text = speaker.speakerName;
cell.lblSpeakerCity.text = speaker.speakerCity;
return cell;
}
When I add a search bar and define searchBar textDidChange event, I successfully get items that contains the key letters. But I can’t re-populate my table because I only have searched items’ names.
Tutorials I got help from are built on default cells and the datasource of tableview is NSString array. But my array is NSObject array, this is my problem.
I consider getting indexes of searched items but how can I use these values either? Do you have any idea or any related link?
I think you should take a look at Bobgreen’s blog, his tutorial helped me quite a lot.
http://www.cocoabob.net/?p=67
Check his delegeate function for UISearchDisplayController (which I guess you might want to use?)
He has an array of NSObject’s (
product) there which he loops thru. Each time a result matches the search string he puts the value in an second array calledfilteredListContentand he’ll show upp the filtered content. Works like a charm for me.