【问题标题】:Autocomplete textfield for years in IOS多年来在 IOS 中自动完成文本字段
【发布时间】:2017-10-04 09:38:26
【问题描述】:

我有一个文本字段,用户想在其中输入年份。我在文本字段下有表格视图,我在其中传递了多年的数组。当用户输入年份时,表格视图应该在表格视图中显示来自数组的年份列表,并且它应该匹配数组中的前两个单词。我尝试了一个代码,但它没有显示其中的年份数组。我的代码是这样的,

-(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];
year1Array = [year1Array filteredArrayUsingPredicate:predicate];
NSLog(@"%@", year1Array);

if ([year1Array count]==0) {
    _year01.hidden=true;
}else{
    _year01.hidden = FALSE;
}

[_year01 reloadData];


return TRUE;

}

tableview代码是这样的,

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView==_year01) {
        return [year1Array count];
    }else{
        return [year2Array count];
    }
    return YES;
}



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    if (tableView == _year01){
        cell.textLabel.text=[year1Array objectAtIndex:indexPath.row];
    }else{
        cell.textLabel.text=[year2Array objectAtIndex:indexPath.row];
    }

    cell.backgroundColor=[UIColor grayColor];
    cell.textLabel.textColor=[UIColor whiteColor];
   return cell;

}

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];

    if (tableView == _year01){
        self.year1.text=[year1Array objectAtIndex:indexPath.row];
        self.year01.hidden=YES;
    }else{
        self.year2.text=[year2Array objectAtIndex:indexPath.row];
        self.year02.hidden=YES;
    }
}

【问题讨论】:

    标签: ios objective-c nsarray


    【解决方案1】:

    只需将CONTAINS[cd] 更改为NSPredicate

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

    已编辑:

    知道你哪里出错了

    year1Array = [year1Array filteredArrayUsingPredicate:predicate];
    

    您正在将过滤后的结果添加到 year1Array 您的主数组,因此当您无法获得结果时意味着 0 那么您的主数组也将为 0,这就是您面临此类问题的原因。

    所以取一个全局数组作为 Ex.

    @property (nonatomic, strong) NSMutableArray *temStaticArray;

    只将您的 year1Array 添加到 temStaticArray 一次。

    [temStaticArray addObjectsFromArray:year1Array];
    

    现在更改shouldChangeCharactersInRange中的逻辑

    if (year1Array.count > 0){
       [year1Array removeAllObjects];
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"SELF CONTAINS %@",passcode];
    year1Array = [temStaticArray filteredArrayUsingPredicate:predicate];
    NSLog(@"%@", year1Array);
    
    if ([year1Array count]==0) {
       [year1Array addObjectsFromArray:temStaticArray];
        _year01.hidden=true;
    }else{
        _year01.hidden = FALSE;
    }
    
    [_year01 reloadData];
    

    【讨论】:

    • 让我试试。 @iPatel
    • 兄弟,当我重新输入它不显示 tableview 的年份时,它只有一次显示 table view。 @iPatel
    • 无法根据你的情况得到你 if ([year1Array count]==0) { _year01.hidden=true; }else{ _year01.hidden = FALSE;如果它是真的,那么你的表格将被显示,否则它将被隐藏
    • 也可以尝试使用BEGINSWITH[cd] 而不是CONTAINS[cd]
    • 它显示和隐藏很好,但是当用户想要从文本字段中删除所有内容并再次尝试输入时,它不会显示表格视图。 @iPatel
    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 2019-10-25
    • 1970-01-01
    • 2010-10-15
    • 2015-04-27
    • 1970-01-01
    相关资源
    最近更新 更多