坑1.点击搜索框后,搜索框上移到屏幕之外了
不多说了,都是坑
因为我之前设置了[UINavigationBar appearance] = false;
把这个去掉好了
坑2.搜索时搜索框和UITableview中间有个44的空白
也是一句话,添加上就好了
self.definesPresentationContext = YES; // know where you want UISearchController to be displayed
知识点3.多维度匹配
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
// update the filtered array based on the search text
NSString *searchText = searchController.searchBar.text;
NSMutableArray *searchResults = [self.dataArray mutableCopy];
// strip out all the leading and trailing spaces
NSString *strippedString = [searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
// break up the search terms (separated by spaces)
NSArray *searchItems = nil;
if (strippedString.length > 0) {
searchItems = [strippedString componentsSeparatedByString:@" "];
}
// build all the "AND" expressions for each value in the searchString
//
NSMutableArray *andMatchPredicates = [NSMutableArray array];
for (NSString *searchString in searchItems) {
// each searchString creates an OR predicate for: name, id
NSMutableArray *searchItemsPredicate = [NSMutableArray array];
// use NSExpression represent expressions in predicates.
// NSPredicate is made up of smaller, atomic parts: two NSExpressions (a left-hand value and a right-hand value)
// friendName field matching
NSExpression *lhs = [NSExpression expressionForKeyPath:@"name"];
NSExpression *rhs = [NSExpression expressionForConstantValue:searchString];
NSPredicate *finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// friendId field matching
lhs = [NSExpression expressionForKeyPath:@"id"];
rhs = [NSExpression expressionForConstantValue:searchString];
finalPredicate = [NSComparisonPredicate
predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:NSContainsPredicateOperatorType
options:NSCaseInsensitivePredicateOption];
[searchItemsPredicate addObject:finalPredicate];
// at this OR predicate to our master AND predicate
NSCompoundPredicate *orMatchPredicates = [NSCompoundPredicate orPredicateWithSubpredicates:searchItemsPredicate];
[andMatchPredicates addObject:orMatchPredicates];
}
// match up the fields of the Product object
NSCompoundPredicate *finalCompoundPredicate =
[NSCompoundPredicate andPredicateWithSubpredicates:andMatchPredicates];
searchResults = [[searchResults filteredArrayUsingPredicate:finalCompoundPredicate] mutableCopy];
// hand over the filtered results to our search results table
ZYQSearchResultController *tableController = (ZYQSearchResultController *)self.searchController.searchResultsController;
tableController.dataArray = searchResults;
[tableController.tableView reloadData];
}