iPhone 中的高级搜索实现?

发布于 2024-10-03 04:36:14 字数 87 浏览 1 评论 0原文

我想实现高级搜索功能,以便如果在搜索栏中输入特定文本,则应根据搜索过滤 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑饮青盏花 2024-10-10 04:36:14

使用 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.

梦一生花开无言 2024-10-10 04:36:14

我自己没有尝试过类似的方法,但我的方法如下:

  1. 在您的 UITextFieldDelegate 对象中实现方法 textField:shouldChangeCharactersInRange:replacementString: 。对于从 textField 输入或删除的每个字符,都会调用此方法。

  2. 在上面的委托方法中,根据 TextField 中的当前文本内容,执行搜索并返回结果列表(数组)。

  3. 获取数组并为每个结果构造一个自定义 UITableViewCell。该单元格应该有一个 UIWebView,其文本是搜索结果的摘录。

  4. 为了突出显示,您需要在要在 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:

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文