UISearchbar results filtering
I have a UISearchBar where the user can type the text. Basically it is a dynamic company name search. (loaded from ext API) e.g. if the user has typed "MIC", it will show AMIC, BMIC, CMIC, ...MICROSOFT.
I am using the code below
NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];
My ques开发者_运维技巧tion is; 1. Is this the best approach for filtering? i.e. For comp name should I use Name starts with OR Name contains, which is the current approach. 2. The search is not working for spaces. e.g. it returns no results for "COMP NAME"
I'll add comments for additional info as I get answers. Please suggest.
With only one line of code to go on, it's hard to tell whether or not your search implementation is the "best approach" - providing additional info would be helpful. For example, we don't even know where the sTemp
variable is coming from or what it contains.
That said:
- If your results are indeed coming from an external API, it might provide you a speedup to send the search string to that API and have it do the search closer to the data source - having you cache and do a string scan on your entire result set will show some performance slowdowns as your data set size increases. If you're set on doing the search locally, though, then yes, a case-insensitive search is probably your best option.
- Do you expect results for "COMP NAME"? I'm going to wildly assume that you have some company called "COMPANY NAME", and want your abbreviated "COMP NAME" search to hit on "COMPANY NAME". Then your problem here is that you're doing a compare on the entire string, not parts of it - "COMPANY NAME" doesn't contain the substring "COMP NAME", even in case-insensitive comparison. What you need to do instead is split your search string on whitespace, then check for each company if the name contains all the substrings of your query. In this example, then, "COMPANY NAME" contains both the substrings "COMP" and "NAME", so it would match.
精彩评论