0

で検索したい のですNSMutalbleArrayが、コードは次のとおりです。

    arrCelebs=[[NSMutableArray alloc] initWithObjects:@"Test 1",@"Test 2",@"Test 42",@"Test 5", nil];

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *arrString1 = txtSearch.text;
    NSRange tmprange;
    for(NSString *string in arrCelebs) {
            tmprange = [arrString1 rangeOfString:string];
            if (tmprange.location != NSNotFound) {
                     NSLog(@"String found");
                    break;
            }
    }
    return YES;
    }

「t」と入力すると、すべてのデータが検索され、別の配列に追加したいと思います。テーブルビューでの表示用。

4

6 に答える 6

7

の力を利用できますNSPredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@", textField.text];
NSArray *filteredArray = [arrCelebs filteredArrayUsingPredicate:predicate];
于 2013-03-15T13:46:55.913 に答える
3

述語を使用することで、簡単に効率的に結果を得ることができます。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] %@",  txtSearch.text];
    NSArray *ResultArray = [yourArray filteredArrayUsingPredicate:predicate];
    return YES;
}
于 2013-03-15T13:52:31.927 に答える
0
arrCelebs=[[NSMutableArray alloc] initWithObjects:@"Test 1",@"Test 2",@"Test 42",@"Test 5", nil];

_resultArray = [[NSMutableArray alloc] init];

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSString *arrString1 = txtSearch.text;
NSRange tmprange;
_resultArray = [[NSMutableArray alloc] init];
for(NSString *string in arrCelebs) {
        tmprange = [arrString1 rangeOfString:string];
        if (tmprange.location != NSNotFound) {
                 [_resultArray addObject:string];
        }
}
return YES;
}
于 2013-03-15T13:50:08.577 に答える
0

あなたが一緒に働くUITableViewなら、あなたはまたこのタイプのロジックを入れます。

TwoNSMutableArrayを取り、次のようなメソッドで1 つの配列を別の配列に追加します。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-03-15T14:00:38.080 に答える
0

試す :

arr_NewArray  = [[NSMutableArray alloc] init];

for (int i = 0; i < [arr_YourArrayToSearch count]; i++)
{
     NSString *curString = [string lowercaseString];
     NSString *curStringInArray = [[arr_YourArrayToSearch objectAtIndex:i]lowercaseString];

     if (![curString rangeOfString:curStringSmall].location == NSNotFound)
     {
          [arr_NewArray addObject:[arr_YourArrayToSearch objectAtIndex:i]];
     }
 }

arr_NewArray は、検索文字列に一致するデータを含む配列を提供します。

于 2013-03-15T13:46:42.037 に答える
0

これはまさにあなたが望むものです...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSMutableArray *resultAry = [[NSMutableArray alloc] init];
    for(NSString *string in arrCelebs)
    {
        NSRange range = [string rangeOfString:textField.text options:NSCaseInsensitiveSearch];

        if(range.location != NSNotFound)
        {
            [resultAry addObject:string];
        }
    }
    yourTableAry=[resultAry mutableCopy];
    [yourTable reloadData];
    return YES;
}
于 2013-03-15T13:58:59.447 に答える