-3

我的数组就像

{
    "Samsung Tab",
    "Samsung Note",
    "Samsung Galaxy",
    "Samsung Galaxy Pro",
    "Nokia Lumia",
    "Nokia 5130",
    "Sony Xperia"
}     

诸如此类的东西。我有文本框类型 GALAXY 并单击按钮。我只想在下一个列表视图中显示 Samsung Galaxy 、 Samsung Galaxy Pro。谁能帮我?。

4

5 回答 5

2

使用谓词过滤数组,如下所示

NSArray *arrayMobiles= [NSArray arrayWithObjects:@"Samsung Tab",@"Samsung Note", @"Samsung Galaxy", @"Samsung Galaxy Pro", @"Nokia Lumia", @"Nokia 5130",@"Nokia 5130",@"Sony Xperia", nil];
NSString *strSearchkey = @"GALAXY";
NSPredicate *containPred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", strSearchkey];
NSArray *arrayFilter = [arrayMobiles filteredArrayUsingPredicate:containPred];
NSLog(@"%@",arrayFilter);
//output 
"Samsung Galaxy",
"Samsung Galaxy Pro"
于 2013-02-11T06:45:54.830 回答
1

这可能会帮助你

- (void)searchArrayFrom: (NSString *) matchString{

NSString *upString = [matchString uppercaseString];
if (searchArray){[searchArray release];searchArray = nil;}
searchArray = [[NSMutableArray alloc] init];
NSMutableArray *temp = [internalEvents copy];

for (int i=0;i<[temp count];i++)
{
    NSString *str = [internalEvents objectAtIndex:i];
    // Add everyone when there's nothing to match to
    if ([matchString length] == 0)
    {
        [searchArray addObject:str];

        continue;
    }

    // Add the person if the string matches

    NSRange range = [[str uppercaseString] rangeOfString:upString];
    if (range.location != NSNotFound) {
        [searchArray addObject:str];
    }
}


[temp release];
temp = nil;
[tblView reloadData];

}

于 2013-02-11T06:26:59.710 回答
1

取两个NSMutableArray并将一个数组添加到另一个数组ViewDidLoad,例如,

self.listOfTemArray = [[NSMutableArray alloc] init]; // array no - 1
self.ItemOfMainArray = [[NSMutableArray alloc] initWithObjects:@"YorArrayList", nil]; // array no - 2 

[self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray]; // add 2array to 1 array

并编写以下 UISearchBar 的委托方法

- (BOOL) textFieldDidChange:(UITextField *)textField
 {
        NSString *name = @"";
        NSString *firstLetter = @"";

    if (self.listOfTemArray.count > 0)
         [self.listOfTemArray removeAllObjects];

        if ([searchText length] > 0)
        {
                for (int i = 0; i < [self.ItemOfMainArray count] ; i = i+1)
                {
                        name = [self.ItemOfMainArray objectAtIndex:i];

                        if (name.length >= searchText.length)
                        {
                                firstLetter = [name substringWithRange:NSMakeRange(0, [searchText length])];
                                //NSLog(@"%@",firstLetter);

                                if( [firstLetter caseInsensitiveCompare:searchText] == NSOrderedSame )
                                {
                                    // strings are equal except for possibly case
                                    [self.listOfTemArray addObject: [self.ItemOfMainArray objectAtIndex:i]];
                                    NSLog(@"=========> %@",self.listOfTemArray);
                                }
                         }
                 }
         }
         else
         {
             [self.listOfTemArray addObjectsFromArray:self.ItemOfMainArray ];
         }

        [self.tblView reloadData];
}

}

输出显示在您的Consol.

此代码可能对您有帮助...谢谢:)

于 2013-02-11T06:32:20.507 回答
1

您可以使用以下给定的功能:

- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring
{
        for (int i = 0; i < [yourArray count]; i++)
        {
            NSString *curString = [[yourArray objectAtIndex:i]lowercaseString];
            NSString *searchString = [substring lowercaseString];

            if ([curString rangeOfString:curStringSmall].location == NSNotFound)
            {
            }
            else
            {
                //This means searched text is found in your array. you can store it in new array. Which will give you only the search criteria matched element.
            }
        }
}

您需要在单击搜索按钮时调用此函数。像 :

-(void)searchButtonClicked
{
     [self searchAutocompleteEntriesWithSubstring:txtSearch.text];
}
于 2013-02-11T06:32:30.643 回答
0

你可能会使用 NSPrediction

NSString * SEARCH_KEYWORD = @"s";

NSMutableArray *array =
    [NSMutableArray arrayWithObjects:@"Bill", @"Ben", @"Chris", @"Melissa", nil];

NSPredicate *sPredicate =
   [NSPredicate predicateWithFormat:@"SELF contains[c] '%@'", SEARCH_KEYWORD];
[array filterUsingPredicate:sPredicate];
// array now contains { @"Chris", @"Melissa" } 
于 2013-02-11T06:40:45.240 回答