Domanda

I have an NSMutableDictionary (PerosnsListSections) and a class names Persons

NSMutableDictionary:

  • Keys are letters like "a,b,m ..."
  • Values are NSMutableArray -> NSMutableArray have objects of class Persons

Persons class: @property (assign,nonatomic) NSInteger pid; @property (strong,nonatomic) NSString *name;

Now i have PerosnsListSections displayed in UITableView as shown in the image

enter image description here

what I want to achieve is when user types in the search bar first i have to filter the section which is the first letter then to filter the names under that section.

Sorry for my bad English (:

È stato utile?

Soluzione

You can first select the correct array in the dictionary by doing:

NSString *firstLetter = [searchString substringWithRange:(NSRange){0,1}];
NSArray *people = PersonsListSection[firstLetter];

Then you can filter down on the people by using NSPredicates:

NSPredicate *namesBeginningWithKeyword = [NSPredicate predicateWithFormat:@"(name     BEGINSWITH[cd] $letter)"];
NSArray *filteredPeople = [people filteredArrayUsingPredicate:[namesBeginningWithKeyword predicateWithSubstitutionVariables:@{@"letter": searchString}]]);

The question of how you make that be reflected in the tableview's content is a whole another question though.

  1. Typically, you'll want your view controller to be the UISearchBar's delegate and react to change using the – (void)searchBar:textDidChange: delegate method.
  2. There you could just call your tableview's - reloadData method so it tries recompute its content, calling all of its dataSource method like - numberOfSectionsInTableView: and so forth.
  3. In these methods, in turn, you'll want to check whether some text was entered in the search bar and use the above tips to return the right sections/cells.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top