views:

508

answers:

5

(Note: This is an extension of a previous question.)

I am having some difficulty implementing a SearchBar for fairly complex tableview. The tableView has multiple sections, two lines of text and an image. All the data is loaded from a plist and then put into sections by the initial letter of the value for the "Name" key. NSLog returns the following for my main dictionary "sectionedDictionaryByFirstLetter":

sectionedDictionaryByFirstLetter:{
B = (
  {
    Name = "B...A Name Starting with B";
    Image = "ImageName1.png";
    Text = "Some Text";
  }
    );
C = (
  {
    Name = "C...A Name Starting with C";
    Image = "ImageName2.png";
    Text = "Some Text";
  }
    );
N = (
  {
    Name = "N1...A Name Starting with N";
    Image = "ImageName3.png";
    Text = "Some Text";
  },
  {
    Name = "N2...A Name Starting with N";
    Image = "ImageName4.png";
    Text = "Some Text";
  },
  {
    Name = "N3...A Name Starting with N";
    Image = "ImageName5.png";
    Text = "Some Text";
  }
  );
}

I want to filter the sub dictionaries by the value for the "Name" key. Can anyone recommend either how to go about this or some resources for learning how? I've been following the example in "Beginning iPhone 3 Development" but haven't been able to translate it to my situation. So if a user types "with N" in the search bar, I would like the new dictionary to appear as:

    filteredSectionedDictionaryByFirstLetter:{
N = (
  {
    Name = "N1...A Name Starting with N";
    Image = "ImageName3.png";
    Text = "Some Text";
  },
  {
    Name = "N2...A Name Starting with N";
    Image = "ImageName4.png";
    Text = "Some Text";
  },
  {
    Name = "N3...A Name Starting with N";
    Image = "ImageName5.png";
    Text = "Some Text";
  }
  );
}

Just to clarify, I have my searchBar installed in the view, I'm just having trouble making it functional. I'm currently toying with a custom method called - (void)handleSearchForTerm:(NSString *)searchTerm from the example in the book.

Thanks for the help!

A: 

Update

It looks like you have arrays keyed by Letter in the array.

I would probably use an NSPredicate and filter on whatever key I was interested in.

NSString *searchString = @"Nancy";

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Name ==  %@)", Nancy];

NSArray *filteredArray = [[sectionedDictionaryByFirstLetter objectForKey:@"N"] filteredArrayUsingPredicate:predicate];

There is a guide on predicate construction.

There are other ways but that is the first that comes to mind.


Original Answer

You can use KVC method:

valueForKeyPath:

To get what you want. You should read up on the KVC apple docs:

http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/

There are many ways "play" with dictionaries/arrays to do what you want. Different techniques will have different strengths/advantages.

Corey Floyd
Thanks! I'll take a look.
Jonah
Do you have any more specific ideas? I'm having a heck of a time digging through all this material. Thanks!
Jonah
Thanks so much for the help! This seems to be on the right track. However, I want to search within every initial letter, not just "N" as in your example. I also want the initial letters to remain in the dictionary only if they contain items. I edited the question to add a bit more clarity.
Jonah
You can perform the above search on every dictionary by iterating through the keys then aggregate the results. I would consider restructuring your data if these are the types of searches you want to perform, however. Why even key them by initial letter? You can always search for the initial letter if needed OR you can make the initial letter a key-value-pair within each individual dictionary.
Corey Floyd
A: 

Here's the general structure of the data.

sectionedDictionaryByFirstLetter

Key   Value
B    Array of Dictionaries with Names beginning with B
      ....
C    Array of Dictionaries with Names beginning with C
      ...
N    Array of Dictionaries with Names beginning with N
     Item 1   Dictionary 1
     Item 2   Dictionary 2
     Item 3   Dictionary 3

          Dictionary 3
          {
          Name = "N3...A Name Starting with N";
          Image = "ImageName5.png";
          Text = "Some Text";
          }

I hope this is of some help. I'm still desperately looking for an answer to this problem.

Essentially, there is a primary dictionary who's keys are the first letters of the items it contains. Within each first letter, there is an array of dictionaries that have a value for their "Name" key that begins with that letter.

Jonah
A: 

Another idea I've had is to create an array of all the values for the "name" key. Something like this:

arrayOfNames:(
"B...A Name Starting with B",
"C...A Name Starting with C",
"N1...A Name Starting with N",
"N2...A Name Starting with N",
"N3...A Name Starting with N"
)

Then, run the filter on this array. Finally, use a filteredArrayOfNames to add the items to a new dictionary that have matching names. Does this seem to be conceptually on the right track? Is there a better route?

Jonah
+1  A: 

Your data structure is messing you up.

Just make a single array of all the dictionaries that you want to show, and then keep it sorted. Breaking it into subletters is just confusing the issue.

So, you'd have an array like this:

[[NSArray alloc] initWithObjects:
  [[NSDictionary alloc] initWithObjectsAndKeys: 
    @"a1", @"name", foo, @"bar", nil)],nil],
  [[NSDictionary alloc] initWithObjectsAndKeys: 
    @"b21", @"name", foo, @"bar", nil)],nil];

Then it's simple. Just filter your sorted array by the input in the search bar. You can even keep your entries in a CoreData database, end declare a function that does something like this to get them:

if (!self.wordList) {
  self.wordList = [self getWiordList]
} else {
  return self.wordList;
}

Also, I'd suggest you create an NSObject subclass, and use an array of those instead of dictionaries, so that you an do dot access and the syntax is nice. Working with dicts and lists in Obj C isn't nice like it is in Python.

Subclassing an NSObject is simple.

Andrew Johnson
Thanks for your answer. The reason I'm using a dictionary broken into subletters is to display a sectioned table view. Right now the table displays beautifully with all the cells sorted and under the correct section with titles for each letter as well as a section index. I'm not sure how I would recreate this design with the data structured how you suggest.However, I could restructure it how you recommend for filtering and then change it back to the current format for displaying the data.Also, I may have neglected to mention that the data are stored in a plist.
Jonah
A: 

Okay, I finally answered my own question. Here is the code below. I mostly followed the example for searching an array in "Beginning iPhone 3 Development". I had to edit it slightly to make it work for my problem. Here is my code:

- (void)handleSearchForTerm:(NSString *)searchTerm
{   
NSMutableArray *sectionsToRemove = [[NSMutableArray alloc] init];
[self resetSearch];
for (NSString *key in self.keys)
{
 NSMutableArray *array = [self.copyOfSectionedDictionaryByFirstLetter valueForKey:key];
 NSMutableArray *toRemove = [[NSMutableArray alloc] init];
 for (NSDictionary *name in array)
 {
  if ([[name objectForKey:@"Name"] rangeOfString:searchTerm options:NSCaseInsensitiveSearch].location == NSNotFound)
   [toRemove addObject:name];
 }

if ([array count] == [toRemove count])
 [sectionsToRemove addObject:key];
[array removeObjectsInArray:toRemove];
[toRemove release];
}

[self.keys removeObjectsInArray:sectionsToRemove];
[sectionsToRemove release];
[self.tableView reloadData]; 
}

If you would like to see the full sample code to see how this is working, you can download from the "Beginning iPhone 3 Development" site here. It is the "sections" example.

Jonah