0

我必须通过引入搜索关键字并在运行时过滤来过滤一组对象。我该怎么做?

//this is my Article class
@property  int intID;
@property (nonatomic, strong) NSString *strName;
@property (nonatomic, strong) NSString *strTitle;
@property (nonatomic, strong) NSString *strDescription;
@property (nonatomic, strong) NSString *lang;
@property (nonatomic, strong) NSString *dateDate;
@property (nonatomic, strong) NSData *dataPicture;
@property (nonatomic, strong) NSString *link;
@property (nonatomic, strong) NSString *gallery;
@property int flag;
@property int subcat;
@property int cat;
@property int idpic;

我必须选择包含带有引入字符串的文本的文章

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{   
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"strName MATCHES '.*(%@).*'", searchText];
    
    NSLog(@"%@", [NSString stringWithFormat:@"strName MATCHES '.*%@.*' OR strTitle MATCHES '.*%@.*'", searchText, searchText]);
    
    articleSearchArray=[NSMutableArray array];
    articleSearchArray = [NSMutableArray arrayWithArray:[allArticleArr filteredArrayUsingPredicate:predicate]];

    for(Article *article in articleSearchArray)
    {
        
        NSLog(@"Article id is %i", article.intID);
    }
    [mytableView reloadData];
    
}
4

1 回答 1

3
[NSPredicate predicateWithFormat:@"strName contains[c] '%@'", searchText];

这将使用不区分大小写的比较(strName用 之后,您只需将哪个作为您的数据源设置为过滤后的数据源并执行.searchText[c]NSArrayreloadData

于 2012-05-22T10:36:27.783 回答