Search code examples
iosuitableviewsearchnsrange

Searching tableview using NSRange with characters from phone pad


I am trying to filter a table view of contacts (name and phone number) using the phone pad (old dumb-phone sms-style mapping from digits to characters) where a search for, say, "3862" could yield 3862, dumb, duna,fvoc... and so on. I have implemented the numeric search just fine using NSRange, but how would I go about the character search?

For clarification, the mappings are:

2 -> (abc),
3 -> (def), 
4 -> (ghi),
...
9 -> (wxyz).

Solution

  • You probably want to encode the strings in question to be numeric values. From there, you can treat both the phone numbers and words the same. If you are going to be rolling your own, you may want to look at the Trie data structure ( http://en.wikipedia.org/wiki/Trie ). Alternatively, you could use something like SQLite or Core Data to store the encoded values and do the retrieval with a "like" statement.