2017-10-04 102 views
0

我有一个用户想要在其中输入年份的文本框。我在textfield下面有一个tableview,我在其中传入了一个数组。当用户输入年份时,表格视图应该在表格视图中显示从数组开始的年份列表,并且它应该匹配数组中的前两个单词。我试过了一段代码,但它并没有显示它的年数。我的代码是这样的,IOS中的自动完成textfield多年

-(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; 

} 

的实现代码如下代码是这样的,

- (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; 
    } 
} 

回答

0

只要改变CONTAINS[cd]NSPredicate

​​

编辑:

了您发出哪里你错了

year1Array = [year1Array filteredArrayUsingPredicate:predicate]; 

要添加滤波的结果year1Array你的主阵列,所以当你不会得到结果意味着0那么你的主阵列也将是0这就是为什么你面对这种类型的问题。

所以采取一个全球阵列为例。

@property(nonatomic,strong)NSMutableArray * temStaticArray;

添加year1ArraytemStaticArray只有一次喜欢。

[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]; 
+0

让我试试。 @iPatel – Raheel

+0

Bro只有一次显示表格视图,当我重新回到不显示tableview的那一年时。 @iPatel – Raheel

+0

如果([year1Array count] == 0)无法获得您的条件if { _year01.hidden = true; } else { _year01.hidden = FALSE; } 如果它是真的,那么你的表格将被显示,否则它将被隐藏 – iPatel