2017-08-26 92 views
0

我尝试了所有的东西,但它不起作用。我有负载plist tableview。我正在尝试实现搜索功能。如何实现在plist中搜索字符串?

有我的代码,

#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> 

我等待着您的咨询,谢谢

+0

添加以下代码并检查:NSPredicate * predicate = [NSPredicate predicateWithFormat:@“SELF CONTAINS [cd]%@”,self.searchBar.text]; –

+0

不,不起作用。应用程序刚刚崩溃 – ahmetberkaycalisti

回答

0

您搜索阵列(电影)的NSDictionary 的所以使用您在谓语使用SELF.keyname从NSDionary

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); 
+0

它仍然无法正常工作并出现错误。任何想法? – ahmetberkaycalisti

+0

秒我会把它写为代码 –

+0

@ahmetberkaycalisti检查它的工作更新代码,并得到结果,你可能有错误显示结果数据 –