UISearchBar & Custom Table Cells
I’ve been working on an iPhone app that involves using the search bar. This is my first time using the search bar, so I’ve collected bits of code here and there from tutorials and reading questions on Stack Overflow. I can’t find the original tutorial that I used, but this one is strikingly similar.
The problem I was having is that I am using a Custom Cell for the Table View which displays data from 2 different dictionaries. A primary label, subtitle, and an image:
The - (void)handleSearchForTerm:(NSString *)searchTerm method from the tutorial was built to search and present data from a single NSMutableDictionary. It was successfully eliminating the data from the dictionary containing the primary label when the search terms changed, but not eliminating the data from the secondary label’s dictionary and the image file dictionary (e.g. when I typed “V”, werewolf and ghost were eliminated from their respective dictionary, but not “Howls at the moon” and “Is transparent and haunts you”. And also the werewolf and ghost images were not removed). The result was that when I searched “Vampirism”, the primary label “Vampire” displayed, but the secondary label and the image displayed were from the first entries in their respective dictionaries. I could search “Vampirism” and it would display a picture of a centaur (the first entry in the dictionary), the primary label “Vampirism” and the secondary label “Half-man, half-horse”.
After probably over 20 hours of struggling with this, I finally found a solution. It’s not elegant, and I still am not able to search by both “Monster Name” and “Monster Description”
The solution was to create an NSMutableIndexSet to get the index numbers that correspond to the monster being removed from the “monsters” NSMutableDArray. I could then use that index set to remove the objects from the other two arrays.
Please feel free to reply with any questions, or even to tell me how there is a much better way to do this. And definitely let me know if you have a hint for how to search by text in either the Monster or Description arrays. That will be my next project to solve, and once I tackle it, I will post how I did it.