iOS 模糊搜索

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"%@",searchText);
    if (self.dataArr.count==0) {
        return;
    }
    
    if (searchText.length) {
        self.isSearch = YES;
        [self.resultArr removeAllObjects];
        NSPredicate *predicate;
        predicate = [NSPredicate predicateWithFormat: @"shiche_nickname CONTAINS %@" ,searchText];
        
        NSArray *temp = [self.dataArr filteredArrayUsingPredicate:predicate];
        self.resultArr = [[NSMutableArray alloc] initWithArray:temp];
        [self.messageTableView reloadData];
    }else
    {
        self.isSearch = NO;
        [self.messageTableView reloadData];
    }
    
}

[NSPredicate predicateWithFormat: @"shiche_nickname CONTAINS %@" ,searchText]
shiche_nickname 是属性名
如果没有属性 用SELF

你可能感兴趣的:(iOS 模糊搜索)