Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use UISearchBar for Custom Cells in UITableView

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?

like image 465
kubilay Avatar asked Nov 27 '25 01:11

kubilay


1 Answers

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?)

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
   [self.filteredListContent removeAllObjects];
    for (Product *product in listContent)
  {
    if ([scope isEqualToString:@"All"] || [product.type isEqualToString:scope])
     {
        NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
        if (result == NSOrderedSame)
        {
         [self.filteredListContent addObject:product];
         }
      }
   }
}

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 called filteredListContent and he'll show upp the filtered content. Works like a charm for me.

like image 180
Paul Peelen Avatar answered Nov 28 '25 17:11

Paul Peelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!