UISearch 重新排列indexpath.row

发布于 2024-08-31 09:37:03 字数 215 浏览 1 评论 0原文

我在 TableView 中实现了一个 UiSearchBar,并且还有两个 NSArray,一个用于标题,一个用于描述。当我搜索标题数组时,它会返回正确的搜索,但是当我单击搜索附带的行时,如果单击第一行,我会得到“第 0 行”。我的问题是如何在两个数组之间建立连接,以便当搜索根据用户搜索重新排列标题时,描述数组对应于标题所在的同一行。

I have a UiSearchBar implemented in my TableView, and I also have two NSArrays, one for title and one for description. When I search through the array of the titles, it returns the right search, but when I click on a row that the search came with, I get "row 0" if I click on the first row. My question is how to make a connection between the two arrays so that when the search rearranges the titles based on the user search, the description array corresponds to the same row the title is at.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

幽蝶幻影 2024-09-07 09:37:03

只需不要使用两个 NSArray,而只使用一个带有自定义 NSObject 对象的 NSArray:

@interface SomeObject : NSObject {
    NSString *_title;
    NSString *_description;
}

- (BOOL)matchesKeywords:(NSString *)keywords;

@end

然后您将所有信息存储在一个类中,这就是 Obj-C 的方式。您可以轻松地执行搜索,因为对象本身有点知道它是否与给定的关键字匹配,因此当您想要更改SomeObject时,您可以轻松地管理这些更改班级本身。

Simply do not use two NSArrays, but just one with custom NSObject objects:

@interface SomeObject : NSObject {
    NSString *_title;
    NSString *_description;
}

- (BOOL)matchesKeywords:(NSString *)keywords;

@end

Then you have all your information stored in one class, the way Obj-C is meant to be. You can easily perform the search because the objects itself sort of knows whether it matches a given keyword, so when you'd like to change SomeObject you can easily manage those changes in the class itself.

最佳男配角 2024-09-07 09:37:03

我确实将两个数组合并为一个,但这使得 tableviewcell 加载速度变慢,因为单元格同时保存了标题和描述

I did merge the two arrays into one, but that made the tableviewcell load alot slower because the cell is holding both, the title and description

强辩 2024-09-07 09:37:03

我曾经遇到过这个问题所以为了快速解决我这样做:
在标题中:
BOOL 使用FilterArray;

在完整字典和过滤字典之间切换时,只需将上面的 BOOL 分别设置为 NO 和 YES 即可。

然后在 didSelectRowAtIndexPath 中使用“if”语句来检查 usingFilterArray 的状态。
休息应该很轻松。 (如果您仍然需要帮助,请告诉我)
只有一件事是,当您在过滤器字典水合后执行搜索时,如果您取消搜索,则需要确保运行此搜索,否则您的应用程序将崩溃,因为水合字典中不会有任何对象。 (我假设你清理了过滤后的字典)

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
        [self.tableView reloadData];
 } 

伙计,这只是一个快速修复。

I had this problem once So for the quick fix I did this:
In the header:
BOOL usingFilterArray;

Where you switch between the complete dictionary and the filtered one simply set the above BOOL to NO and YES respectively.

then in didSelectRowAtIndexPath use "if" statement to check the sate of the usingFilterArray.
Rest should be pretty easy. (Let me know if you still need help)
Just one thing when you perform the search after the filter Dictionary is hydrated if you cancel the search you need to make sure to run this or your app is going to crash as the hydrated dictionary will not have any object in it. (I assumed you cleaned the filtered Dictionary)

- (void) searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
        [self.tableView reloadData];
 } 

Mate this is just a quick fix.

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