我整天都在尝试,但搜索显示委托方法的方法不起作用。
我有一个加载plist的tableview,我正在尝试实现搜索功能。
这是我的代码
#import "TabelViewController.h"
#import "Cell.h"
@interface TabelViewController ()
@property (nonatomic,copy) NSDictionary *names;
@property (nonatomic,copy) NSArray *keys;
@property (nonatomic,copy) NSArray *FIOArray;
@property (nonatomic,copy) NSMutableArray *filtredNames;
@property (nonatomic,copy) UISearchDisplayController *searchController;
@end
剂量正常工作的方法。 我不知道如何从我的plist中正确加载字典来实现搜索
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[filtredNames removeAllObjects];
if (searchString.length > 0) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];
for (NSString *key in keys ){
NSArray *matches = [names[key] filteredArrayUsingPredicate:predicate];
[filtredNames addObjectsFromArray:matches];
}
}
return YES;
}
Plist结构
<plist version="1.0">
<dict>
<key>А</key>
<array>
<dict>
<key>ФИО</key>
<string>Аетров А.В.</string>
<key>Доклад</key>
<string>Доклад 5</string>
<key>Время</key>
<string>10:00-10:30</string>
<key>День</key>
<string>Четверг</string>
</dict>
</array>
答案 0 :(得分:0)
此代码会将您的plist加载到字典中:
NSString * filePath = [[NSBundle mainBundle] pathForResource:@"Your_actual_file_name" ofType:@"plist"];
names = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSMutableArray * allValues = [NSMutableArray new];
for (NSArray * array in dict.allValues) {
[allValues addObjectsFromArray:array];
}
FIOArray = allValues;
然后您的搜索方法将如下所示:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[filtredNames removeAllObjects];
if (searchString.length > 0) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", searchString];
NSArray *matches = [FIOArray filteredArrayUsingPredicate:predicate];
[filtredNames addObjectsFromArray:matches];
}
return YES;
}