开发者

How to Use UISearchBar for Custom Cells in UITableView

开发者 https://www.devze.com 2023-04-09 19:57 出处:网络
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.

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 laye开发者_StackOverflow社区r, 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?)

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号