从目标C数组项自动完成txtfield(AutoComplete txtfield from arr

2019-10-31 05:25发布

我有一个文本框在其中,只要用户类型它显示了阵列相关项目中tableView选择其中任何一个,但是当用户键入关键字小东西它并不显示数组。 当用户用大写字母在一个阵列输入任何单词作为储存它显示了阵列。

我想,当用户在小或大写字母输入任何单词是否应显示包含数组表视图。 以下是我的代码,

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

    NSLog(@"Range:%@",NSStringFromRange(range));
    NSLog(@"%@",textField.text);

    NSString *passcode = [textField.text stringByReplacingCharactersInRange:range withString:string];

    NSLog(@"%@",passcode);


    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF CONTAINS %@",passcode];

    carArray = [_staticCarArray filteredArrayUsingPredicate:predicate];

    city = [_staticCarArrays filteredArrayUsingPredicate:predicate];


    NSLog(@"%@", carArray);
    NSLog(@"%@", city);


    if ([carArray count]==0) {
        _carTable.hidden = TRUE;
    }else{
        _carTable.hidden = FALSE;
    }

    if ([city count]==0) {
        _autotable.hidden = TRUE;
    }else{
        _autotable.hidden = FALSE;
    }

    [_carTable reloadData];
    [_autotable reloadData];


    return TRUE;

}

Answer 1:

对于情况insenstive搜索替换

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF CONTAINS %@",passcode];

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF CONTAINS[c] %@",passcode];

这里,[C]表示不区分大小写的比较。



Answer 2:

使用UITextfieldDeleage,的UITableViewDelegate,UITableViewDataSource

@interface ABCViewController : UIViewController <UITextFieldDelegate> 
myTextField.delegate = self;

1.Get TextField的文本和保存的NSMutableArray价值

2.Retrieve阵列值,并检查文本字段的文本中使用数组已经NSPredicate包含

3.Show的TableViewCell为textLabel文本到文本字段

 #pragma mark - UITextField Delegate
 -(BOOL)textField:(UITextField *)textField 
 shouldChangeCharactersInRange:(NSRange)range replacementString:
                                               (NSString *)string{

 NSLog(@"Range:%@",NSStringFromRange(range));
 NSLog(@"%@",textField.text);

 NSString *passcode = [textField.text stringByReplacingCharactersInRange:range withString:string];

 NSLog(@"%@",passcode);

 NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"SELF CONTAINS %@",passcode];
 autoCompleteFilterArray = [autoCompleteArray filteredArrayUsingPredicate:predicate];
 NSLog(@"%@", autoCompleteFilterArray);

 if ([autoCompleteFilterArray count]==0) {
    autoCompleteTableView.hidden = TRUE;
 }else{
    autoCompleteTableView.hidden = FALSE;
 }
 [autoCompleteTableView reloadData];

return TRUE;
}


文章来源: AutoComplete txtfield from array items in Objective c