0

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. enter image description here It is also updating the id. as shown in image enter image description here

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!

Muju
  • 884
  • 20
  • 54
  • in your didselectRowAtIndexPath method you have compare your array for (int i1=0 ; i1<[YOURORIGNALARRAY count]; i1++) { if ([SEARCHARRAY objectAtIndex:indexPath.row]==[[YOURORIGNALARRAY valueForKey:@"YOURKEY"] objectAtIndex:i1]) { } } – Himanshu Moradiya Oct 12 '16 at 08:44
  • 1. It will be cleaner to have an `NSArray *` for use by the tableview and have that point to the *normal* or *search* arrays depending on whether you are searching or not; this will remove the need for the `if (isFilter)` conditions. 2. Have you made sure the model arrays contain the correct data? 3. How can the name array be the same size as the normal array and the filtered array? – Droppy Oct 12 '16 at 08:47
  • @himanshu why I am getting idarray as 1 while it is 18. – Muju Oct 12 '16 at 09:08
  • Did u show me your array that orignal one and your search array. – Himanshu Moradiya Oct 12 '16 at 09:13
  • @himanshu yes.. – Muju Oct 12 '16 at 09:17
  • @Muju show me your array value . – Himanshu Moradiya Oct 12 '16 at 09:27
  • @himanshu idarray is Printing description of self->idarray: 1,2,3,4,5,6,7,8,9,10, 11, 12, 13, 14, 15, 16, 17, 18, ) – Muju Oct 12 '16 at 09:42
  • Did u create a 2 array for a single person information like name have one array id have different array and id have connected with name – Himanshu Moradiya Oct 12 '16 at 09:44
  • @himanshu name have one array, id have one array and search have one array. – Muju Oct 12 '16 at 09:46
  • then simply create one array with key pair value like . YOURARRAY_NAME =@[@{@"id":@"1" , @"name":@"employee name"}, @{@"id":@"2" , @"name":@"employee name 2"}}]; – Himanshu Moradiya Oct 12 '16 at 09:49
  • @Himanshu I created Dictionary in the same way as you suggest. – Muju Oct 12 '16 at 09:50
  • and when you want perform searching in this array then use NSString *searchTerm = @"search this"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF LIKE[cd] %@", searchTerm]; NSArray *filtered = [array filteredArrayUsingPredicate:predicate]; and use filter array – Himanshu Moradiya Oct 12 '16 at 09:51
  • @HimanshuMoradiya I have change question. – Muju Oct 12 '16 at 12:29
  • @Droppy I have change question please see it. – Muju Oct 12 '16 at 12:30
  • use my array your problem will be slove – Himanshu Moradiya Oct 12 '16 at 12:32
  • @HimanshuMoradiya where to add your code in my code. – Muju Oct 12 '16 at 12:34
  • @HimanshuMoradiya Your Code dosen't search anything.I am searching in dynamic array. – Muju Oct 12 '16 at 12:41
  • @user3182143 Please see the updated question. – Muju Oct 12 '16 at 12:47
  • i just tell you that make your dynamic array that i build in comment. and after it perform searching, – Himanshu Moradiya Oct 12 '16 at 12:56
  • @HimanshuMoradiya OK – Muju Oct 12 '16 at 13:05
  • NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", searchText]; FILETERARRAY = [[YOUROLDARRAY valueForKey:@"ARRAYSEARCHKEY"] filteredArrayUsingPredicate:filterPredicate]; if (FILETERARRAY.count == 0) { searchformember=0; [self.tbl_select_members reloadData]; } [self.tbl_select_members reloadData]; – Himanshu Moradiya Oct 12 '16 at 13:09
  • @HimanshuMoradiya Your code is not working. – Muju Oct 13 '16 at 05:38
  • I just use this code in project and its complete working. – Himanshu Moradiya Oct 13 '16 at 05:40
  • @HimanshuMoradiya I am using two array is that is a problem. – Muju Oct 13 '16 at 05:41
  • @HimanshuMoradiya idarray give me the index tableview cell. But i need value of array my code now is idlbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]]; – Muju Oct 13 '16 at 05:42
  • but why you use 2 array perform with single array give me your email id i give you one demo check it – Himanshu Moradiya Oct 13 '16 at 05:42
  • @HimanshuMoradiya I need it's id to pass in another JSON link. – Muju Oct 13 '16 at 05:44
  • @HimanshuMoradiya mujtabaulhasankhan@rediffmail.com – Muju Oct 13 '16 at 05:45
  • check that demo i create long time ago if any misunderstanding in that send me replay in my mail – Himanshu Moradiya Oct 13 '16 at 05:49
  • @HimanshuMoradiya Can you send your email on this mujtabaulhasankhan@gmail.com . I didn't get your mail. – Muju Oct 13 '16 at 06:12
  • @Muju idArray is not been filtered with name array . so obviosly its gonna give you wrong id . For example : In name array before filtering, at Index 0 there is "a" . you search for "b". now at index 0 its "b" . but in your id array at index 0 its representing "a" reference . – Devang Goswami Oct 13 '16 at 06:17
  • From my point of view you should create dictionary not different array . – Devang Goswami Oct 13 '16 at 06:17
  • @DevangGoswami But table view get crash if I add dictionary in it. – Muju Oct 13 '16 at 06:20
  • @Muju its fail to send and now i try again wait – Himanshu Moradiya Oct 13 '16 at 06:33
  • @Muju check your mail account mujtabaulhasankhan@gmail.com this one i just send you one demo – Himanshu Moradiya Oct 13 '16 at 06:45
  • @Muju your coding style is too poor improve it with your logic brother you send me that domo i just give you down vote . – Himanshu Moradiya Oct 13 '16 at 07:22
  • Possible duplicate of [How to get actual value of NSMutableArray not Index in UITableview in Objective c](http://stackoverflow.com/questions/40036478/how-to-get-actual-value-of-nsmutablearray-not-index-in-uitableview-in-objective) – Muju Oct 14 '16 at 07:19

2 Answers2

0

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

Community
  • 1
  • 1
Purushothaman
  • 358
  • 4
  • 22
  • I am using a same code. But the problem is that this code has only name but my array has name as well as id. It is dictionary. – Muju Oct 12 '16 at 09:55
  • my ID Array also get updated when I search from 18 to 1 – Muju Oct 12 '16 at 10:34
0

Check this file for create one array and perform searching on that array with NSPredicate

https://gist.github.com/himanshu-benzatine/716fd8d7ca035a5c0a01c87df48915bf

Himanshu Moradiya
  • 4,769
  • 4
  • 25
  • 49