I'm new in iOS and when I add search box in my textbox after searching it give me the wrong id. In my code there is three array one is for id another is for name and third one is to search.
My code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sections{
if(tableView == table)
{
if(isFilter)
{
return [searchArray count];
}
else
return [idarray count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == table)
{
if(isFilter)
{
cell.textLabel.text=[NSString stringWithFormat:@"%@",[searchArray objectAtIndex:indexPath.row]];
}
else
{
cell.textLabel.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(tableView == table)
{
if(isFilter)
{
txt.text=[searchArray objectAtIndex:indexPath.row];
table.hidden=YES;
txt.enabled=NO;
txt.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
EmployeeID=idlbl.text;
EmployeeName=txt.text;
}
else
{
txt.text=[NSString stringWithFormat:@"%@",[namearray objectAtIndex:indexPath.row]];
idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
table.hidden=YES;
EmployeeID=idlbl.text;
EmployeeName=txt.text;
txt.enabled=NO;
}
}
}
-(void)textFieldDidChange:(UITextField *)textField
{
searchTextString=textField.text;
[self updateSearchArray:searchTextString];
}
-(void)updateSearchArray:(NSString *)searchText
{
if(searchText.length==0)
{
isFilter=NO;
}
else{
isFilter=YES;
searchArray=[[NSMutableArray alloc]init];
for(NSString *string in namearray){
NSRange stringRange=[string rangeOfString:searchText options:NSCaseInsensitiveSearch];
if(stringRange.location !=NSNotFound){
[searchArray addObject:string];
}
}
[table reloadData];}
}
I'm getting the EmployeeID as 1 where EmployeeID is 18 It is taking the value which is in the table not taking the value of id array. I'm using if(tableview == table) because there is more then one table in one view controller. It is also updating the id. as shown in image
But I do not need to update the idarray. My question is how can I get Value of array it is giving me an indexpath of tableview cell Any Suggetion Thanks in Advance!
When i used to work on the search bar button in my app, i too got same problem , but i have created the textbox which is acting as search bar.
i thought it will be work fine for you also, Happy Coding.......
click here
Check this file for create one array and perform searching on that array with NSPredicate
https://gist.github.com/himanshu-benzatine/716fd8d7ca035a5c0a01c87df48915bf