Advance search implementation in iphone?

拥有回忆 提交于 2019-12-18 03:35:22

问题


I want to implement advance search functionality so that if a particular text is typed in search bar, the list of contents in UITableview should be filtered based on the search and then the search text occurances should be highlighted? Please give me some ideas?


回答1:


Use NSAttributedString... Find UIControllers that draw NSAttribute String because UILabel,UITextView doesnot support NSAttributedString...

Get the controller here: https://github.com/AliSoftware/Ali-Cocoa-Classes/tree/master/OHAttributedLabel

PS: if you plan to distribute an iOS6-only application, as UILabel now support NSAttributedString, you should use UILabel directly instead of OHAttributedLabel as it is now natively supported by the OS.




回答2:


I haven't tried something like this myself but my approach would be as follows:

  1. Implement the method textField:shouldChangeCharactersInRange:replacementString: in your UITextFieldDelegate object. This method will get called for every character that is entered or deleted from your textField.

  2. In the above delegate method, based on the current text content in the TextField, perform your search and return a list(array) of results.

  3. Take the array and for each result construct a custom UITableViewCell. This cell should have a UIWebView whose text is an excerpt of your search result.

  4. For the highlighting, you need to perform a search-replace in the text you are about to render in the WebView to find the search string, say foo for example, and replace it with <b>foo</b> or any other HTML formatting you may wish to apply. You can use the standard NSString API - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement to do the find-replace.

Hope this helps.



来源:https://stackoverflow.com/questions/4208282/advance-search-implementation-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!