iPhone 中的高级搜索实现?
我想实现高级搜索功能,以便如果在搜索栏中输入特定文本,则应根据搜索过滤 UITableview 中的内容列表,然后突出显示搜索文本出现的情况? 请给我一些想法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 NSAttributedString... 查找绘制 NSAttributedString 的 UIController,因为 UILabel、UITextView 不支持 NSAttributedString...
在此处获取控制器:https://github.com/AliSoftware/Ali-Cocoa-Classes/tree/master/OHAttributedLabel
PS:如果您计划分发仅限 iOS6 的应用程序,由于 UILabel 现在支持 NSAttributedString,因此您应该直接使用 UILabel 而不是 OHAttributedLabel,因为操作系统现在原生支持它。
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.
我自己没有尝试过类似的方法,但我的方法如下:
在您的
UITextFieldDelegate
对象中实现方法textField:shouldChangeCharactersInRange:replacementString:
。对于从 textField 输入或删除的每个字符,都会调用此方法。在上面的委托方法中,根据 TextField 中的当前文本内容,执行搜索并返回结果列表(数组)。
获取数组并为每个结果构造一个自定义 UITableViewCell。该单元格应该有一个 UIWebView,其文本是搜索结果的摘录。
为了突出显示,您需要在要在 WebView 中呈现的文本中执行搜索替换以查找搜索字符串,例如 foo,并将其替换为
foo< /b>
或您可能希望应用的任何其他 HTML 格式。您可以使用标准 NSString API- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
进行查找替换。希望这有帮助。
I haven't tried something like this myself but my approach would be as follows:
Implement the method
textField:shouldChangeCharactersInRange:replacementString:
in yourUITextFieldDelegate
object. This method will get called for every character that is entered or deleted from your textField.In the above delegate method, based on the current text content in the TextField, perform your search and return a list(array) of results.
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.
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.