我创建了一个SearchBar,它假设是用来过滤我已设置的UITableView的。我正在使用Parse.com将这个数组下拉到我的应用程序中。

- (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view.

     [self Query];

     //[self.searchDisplayController.searchResultsTableView registerNib:[UINib nibWithNibName:@"SearchCell" bundle:nil] forCellReuseIdentifier:@"SearchData"];

}
- (void)didReceiveMemoryWarning {
     [super didReceiveMemoryWarning];
     // Dispose of any resources that can be recreated.
}
-(void)Query {
     PFQuery *Q = [PFQuery queryWithClassName:@"_User"];

     [Q selectKeys:@[@"username"]];

     [Q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
         if (!error) {
              NSLog(@"load Array);
              SearhArray = [[NSArray alloc] initWithArray:objects];
         }
         else{
             NSLog(@"Array No Good");
         }
       }];
}


然后我尝试使用这些代码行过滤数组

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
     self.searchTerm = searchText;
}

-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
     [self.mySearchBar resignFirstResponder];
     //reset to the original array and reload table
     self.filteredCities = SearhArray;
     [self.myTableView reloadData];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
     [self.mySearchBar resignFirstResponder];

     if ([self.searchTerm isEqualToString:@""]) {
         // Nothing to do here - Reset to the original array and reload table
         self.filteredCities = SearhArray;
     } else {
         // Filter the array based on the term searched for using a predicate
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.title contains[c] %@",self.searchTerm];
         self.filteredCities = [NSArray arrayWithArray:[SearhArray filteredArrayUsingPredicate:predicate]];
     }
     [self.myTableView reloadData];
}

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
     self.filteredCities = SearhArray;
     [self.myTableView reloadData];
}


但是它一直崩溃。我已经将问题调试到了这一行

self.filteredCities = [NSArray arrayWithArray:[SearhArray filteredArrayUsingPredicate:predicate]];


我得到的错误是:

`2015-03-31 19:31:19.783 Pondu[59307:5149459] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Key "words" has no data.  Call fetchIfNeeded before getting its value.'
*** First throw call stack:
(0x18724a59c 0x1979a00e4 0x18724a4dc 0x1000c34bc 0x1880350f0 0x18807b934 0x18807b394 0x18807a084 0x188079e58 0x1000b31b0 0x18be207ac 0x18ba30d34 0x18ba19e48 0x18bbf91dc 0x18bbc3f04 0x18bd80890 0x18bd80540 0x18ba24950 0x18811ddf0 0x1872029ec 0x187201c90 0x1871ffd40 0x18712d0a4 0x1902cf5a4 0x18ba623c0 0x1000b1f64 0x19800ea08)
Libc++abi.dylib: terminating with uncaught exception of type NSException`

最佳答案

self.searchTerm看起来像NSString,但是您放置的是您假设该值为NSDictionary。您需要使用类似


  @“自我匹配%@”


,不是


  @“ SELF.title匹配%@”

07-26 09:39