简体   繁体   English

UISearch重新排列indexpath.row

[英]UISearch rearrange the indexpath.row

I have a UiSearchBar implemented in my TableView , and I also have two NSArray s, one for title and one for description. 我在TableView实现了一个UiSearchBar ,还有两个NSArray ,一个用于标题,一个用于描述。 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. 当我搜索标题数组时,它返回正确的搜索,但是当我单击搜索所伴随的行时,如果单击第一行,则会得到“行0”。 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. 我的问题是如何在两个数组之间建立连接,以便当搜索根据用户搜索重新排列标题时,描述数组对应于标题所在的同一行。

Simply do not use two NSArray s, but just one with custom NSObject objects: 只需不使用两个NSArray ,而仅使用一个带有自定义NSObject对象的对象即可:

@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. 然后,您将所有信息存储在一个类中,这就是Obj-C的含义。 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. 您可以轻松地执行搜索,因为对象本身有点知道它是否与给定的关键字匹配,因此,当您想要更改SomeObject ,可以轻松地在类本身中管理这些更改。

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

I had this problem once So for the quick fix I did this: In the header: BOOL usingFilterArray; 我曾经遇到过这个问题,因此,为了快速修复,我做到了:在标题中:BOOL usingFilterArray;

Where you switch between the complete dictionary and the filtered one simply set the above BOOL to NO and YES respectively. 在完整字典和已过滤字典之间切换的地方,只需将上述BOOL分别设置为NO和YES。

then in didSelectRowAtIndexPath use "if" statement to check the sate of the usingFilterArray. 然后在didSelectRowAtIndexPath中使用“ if”语句检查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. 配合,这只是一个快速修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM