iOS 模糊搜索

赖渊
2023-12-01

- (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 是属性名


 类似资料: