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