如何实现在plist中搜索字符串?

时间:2017-08-26 06:48:05

标签: ios objective-c search plist uisearchcontroller

我尝试了所有的东西,但它没有用。我有负载plist的tableview。我正在尝试实现搜索功能。

有我的代码,

#import <UIKit/UIKit.h>
@interface TableViewController : UITableViewController <UISearchDisplayDelegate, UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic)NSMutableArray *itemList;
@property (nonatomic)NSMutableArray *filteredNames;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) UISearchController *searchController;

当我搜索某些内容时,我可以在nslog函数中看到实体,但tableview

中没有结果
-(void)searchDisplayController:(UISearchController *)controller didLoadSearchResultsTableView:(nonnull UITableView *)tableView
{
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}
-(BOOL)searchDisplayController:(UISearchController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{

    [_filteredNames removeAllObjects];
    if (searchString.length > 0) {
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@",self.searchBar.text];
        NSLog(@"%@",predicate);

        NSString *filePathBundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
        NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:filePathBundle];
        NSArray *films = [root objectForKey:@"FilmsPlaying"];
        for (NSDictionary *dic in films) {

            Films *f = [[Films alloc] initWithDictionary:dic];
            [_filteredNames addObject:f];

        }
    }

        return YES;

}

有我的.m文件

- (void)viewDidLoad {
    [super viewDidLoad];

    _searchController = [[UISearchController alloc]init];

    self.filteredNames = [[NSMutableArray alloc]init];


    [self.tableView  dataSource];
    [self.tableView delegate];
    [self.tableView reloadData];
}

并且有我的plist;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>FilmsPlaying</key>
    <array>
        <dict>
            <key>ZFILMTIME</key>
            <integer>90</integer>
            <key>ZFILMDATESTART</key>
            <date>2015-04-01T09:15:42Z</date>
            <key>ZFILMCONTENT</key>
            <string>bla bla bla </string>
            <key>ZFILMDIRECTOR</key>
            <string>Don Hall,Chris Williams</string>
            <key>ZFILMID</key>
            <string></string>
            <key>ZFILMIMAGE</key>
            <string>bla.jpg</string>
            <key>ZFILMNAME</key>
            <string> Big Hero 6</string>
            <key>ZFILMTYPE</key>
            <string>bla bla bla </string>
            <key>ZFILMVOTE</key>
            <integer>10</integer>
        </dict>
        <dict>
            <key>ZFILMTIME</key>
            <integer>90</integer>
            <key>ZFILMDATESTART</key>
            <date>2015-04-01T09:15:42Z</date>
            <key>ZFILMCONTENT</key>
            <string>bla bla bla .</string>
            <key>ZFILMDIRECTOR</key>
            <string>Frankie Chung</string>
            <key>ZFILMID</key>
            <string></string>
            <key>ZFILMIMAGE</key>
            <string>bla.jpg</string>
            <key>ZFILMNAME</key>
            <string>bla bla bla </string>
            <key>ZFILMTYPE</key>
            <string>bla bla bla </string>
            <key>ZFILMVOTE</key>
            <real>9.5</real>
        </dict>
</array>
</dict>

我在等你的建议,谢谢

1 个答案:

答案 0 :(得分:0)

你搜索NSDictionary的数组(电影)    所以使用你在谓词中使用来自NSDionary的SELF.keyname

NSString *filePathBundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSDictionary *root = [NSDictionary dictionaryWithContentsOfFile:filePathBundle];
NSArray *films = [root objectForKey:@"FilmsPlaying"];

NSLog(@"%@",films);

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.ZFILMNAME CONTAINS[cd] %@", self.searchBar.tex];

NSArray *searchResult = [films filteredArrayUsingPredicate:predicate];
   NSLog(@"%@",searchResult);