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