0

i have a textfield in which user wants to enter the year in it. i have tableview under the textfield in which i have pass an array of years in it . When the user enter the year the table view should display the list of years from array in the table view and it should match the first two words from the array. I have tried a code but it does not show the array of years in it. My code is this,

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

}

the tableview code is this,

    - (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;
    }
}
Vinodh
  • 5,262
  • 4
  • 38
  • 68
Raheel
  • 39
  • 6

1 Answers1

0

Just change CONTAINS[cd] in NSPredicate

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

Edited :

Got you issue where are you going wrong

year1Array = [year1Array filteredArrayUsingPredicate:predicate];

you are adding filtered result to year1Array your main array so when you will not get result means 0 then your main array also will be 0 thats why you are facing that type of issue.

so take one global array for Ex.

@property (nonatomic, strong) NSMutableArray *temStaticArray;

add your year1Array to temStaticArray only once like.

[temStaticArray addObjectsFromArray:year1Array];

Now change your logic in 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
  • 46,010
  • 16
  • 115
  • 137
  • let me try. @iPatel – Raheel Oct 04 '17 at 09:47
  • Bro its only once shows the table view when i reeneter the year it does not show tableview. @iPatel – Raheel Oct 04 '17 at 09:52
  • Can't get you as per your condition if ([year1Array count]==0) { _year01.hidden=true; }else{ _year01.hidden = FALSE; } if it will be true then your table will be displayed otherwise it will be hidden – iPatel Oct 04 '17 at 09:54
  • Also try with `BEGINSWITH[cd]` instead of `CONTAINS[cd]` – iPatel Oct 04 '17 at 09:56
  • it displays and hide fine but when user want to erase all the thing from textfield and try again to enter it does not show tableview. @iPatel – Raheel Oct 04 '17 at 10:00
  • At that time you should check NSLog(@"%@", year1Array); what is display in console – iPatel Oct 04 '17 at 10:06
  • First time when i run and enter any year it shows me this is AAA ( 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010 ) and if i re enter in textfield it shows 0 in year1Array. @iPatel – Raheel Oct 04 '17 at 10:15
  • showing error in this line [year1Array removeAllObjects]; what is removeAllObjects for? @iPatel – Raheel Oct 04 '17 at 10:43
  • Change if ([year1Array count]==0) { [year1Array addObjectsFromArray:temStaticArray]; – iPatel Oct 04 '17 at 10:45
  • check my question . @iPatel – Raheel Oct 04 '17 at 10:52
  • Instead of shouldChangeCharactersInRange method you should try with textfield value change event https://stackoverflow.com/questions/8224221/uitextfield-value-changed-not-firing-when-field-is-updated#14273278 – iPatel Oct 04 '17 at 10:56
  • what to write in the method for change event? @iPatel – Raheel Oct 04 '17 at 11:02