iOS NSPredicate 过滤排序
巴扎黑
巴扎黑 2017-04-18 09:45:32
0
2
380
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = [self.searchController.searchBar text];
    if (searchString.length == 0) {
        return;
    }
    
    NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
    if (self.searchResult!= nil) {
        [self.searchResult removeAllObjects];
    }
    
    //过滤数据
    self.searchResult= [NSMutableArray arrayWithArray:[self.datasource filteredArrayUsingPredicate:preicate]];
    //刷新表格
    [self.tableView reloadData];
}

看例子的的排序都是对简单的字符串数组进行的筛选排序,对于字符串里是对象,按对象里的某个字符串进行筛选,这个可以做到吗?如果不能做到,那么一般用什么办法?以前都是直接用SQL来做的,这次需求里没用这个,能否直接在内容里进行这个
巴扎黑
巴扎黑

reply all(2)
刘奇

That is to say, the array contains model objects, and then matches the string of a certain attribute in the model object?
Match models with a character in the name field in the model
NSPredicate pre = [NSPredicate predicateWithFormat:@"name LIKE 'a*'"];

Ty80

Supporting KVC is enough. If a single object in the Array is a Person, to check whether the name is included, use @"SELF.name CONTAINS[c] %@"

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!