1

我有一个应用程序,其中我有一个像这样创建的 nsmutable 数组。

NSDictionary *memberInfo = [self.currentChannel infoForMemberWithID:memberID];

    for(int i=0;i<self.currentChannel.memberCount;i++)
    {
        [searchfriendarray addObject:memberInfo];   

    }

     NSLog(@"dfdfdfsear%@",searchfriendarray);

我得到的回应是这样的

dfdfdfsear(
        {
        name = baddd;
        status = "<null>";
    },
        {
        name = baddd;
        status = "<null>";
    }
)

` 现在我想在 this.with 方法中搜索一个 nsstring

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText

任何人都可以帮助我实现搜索并根据它加载我的表格视图吗?

4

2 回答 2

4

使用NSPredicate过滤数组。

NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchString];
[yourMutableArray filterUsingPredicate:filterPredicate];
于 2012-10-17T05:57:17.017 回答
0

你可以用NSPredicate这个。

谓词
谓词是过滤数组的有用对象。这有点像在 SQL 中使用简单的 where 子句进行选择。

如果您有这种格式的数组

NSMutableArray *yourAry;
(
        {
        category = classic;
        quote = "Frankly, my dear, I don't give a damn.";
        source = "Gone With the Wind";
    },
        {
        category = classic;
        quote = "Here's looking at you, kid.";
        source = Casablanca;
    }
{
        category = modern;
        quote = "I solemnly swear that I am up to no good.";
        source = "Harry Potter and the Prisoner of Azkaban";
    },
        {
        category = modern;
        quote = "You like pain? Try wearing a corset.";
        source = "Pirates of the Caribbean";
    }
)  

并且您想搜索所有数组,category = classic然后我们可以使用NSPredicate

NSString *selectedCategory = @"classic";

   //filter array by category using predicate
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", selectedCategory];
    NSArray *filteredArray = [self.movieQuotes filteredArrayUsingPredicate:predicate];

如果您需要任何帮助,请关注此博客

于 2012-10-17T08:06:25.413 回答