Abhi February 2016

IOS: getting the value of "id" along with an item is selected through table view

In my project i get response data in the form of

{
  "procedures": [5950]
   0:  {
   "Procedures": {
                  "id": "1"
                  "procedure_name": "3d render w/o postprocess"
                  "procedure_code": "76376"
                 }
       }
  1:  {
        "Procedures": {
        "id": "2"
        "procedure_name": "3d rendering w/postprocess"
        "procedure_code": "76377"
 }

there are 5950 elements in the data array. i create separate arrays for "id" and "Procedure_name" and show data in UITableView it displays correct data with id. I also applied search functionality to filter data as it is so difficult to find any element in 5950 elements by scrolling. search function is also works well but when i select any UITableViewCell of the filtered result it doesn't provide the actual id of that element whereas it returns the current indexpath value of the UITableView. for searching i put the following code

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    tempArray = [NSArray arrayWithArray:dataArray];
    NSString *stringToSearch = textField.text;

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[c] %@",stringToSearch]; // if you need case sensitive search avoid '[c]' in the predicate

    NSArray *tempresults = [dataArray filteredArrayUsingPredicate:predicate];

    if (tempresults.count > 0)
    {
        tempArray = [NSArray arrayWithArray:tempresults];
    }
    [searchdiagtable reloadData];
    return  YES;
}

in DidSelectRow method i apply the following code

if (tableView == searchdiagtable)
    {
        UITableViewCell *selectedCell = [searchdiagtable cellForRowAtIndexPath:indexPath];
        NSLog(@"%@", selectedCell.textLabel.text);
        searchdiag. text = selectedCell.textLabel.t        

Answers


PhaniRaghu February 2016

searchResultId = [iddict objectAtIndex:indexPath.row];

This line of code creates the problem, tableView reuses cells. if you selected the cell , it selects the cell index (0, 1, 2 ... etc). So your array returns the first values. If you are not displaying the "Id" in your table view. You cant get correct id from your array.

There is one solution for this,save id and name in a single dictionary,

like:{
      "id":"name"
}

using the name you can get the id from dictionary.


aman.sood February 2016

checking on your question which I can get is issue must be using a wrong array iddict for search as it looks to me its should be tempArray


PhaniRaghu February 2016

hey you already get two arrays , one for ids and one for names.

How you do that..after response may be you did like this

NSMutableArray *id;
NSMutableArray *name;

    for( ------ ){
    [id addObject:[dict objectforkey:@"id"];
    [name addObjec:[dict objectForKey:@"procedure_name"];
    }

dict is reponse dictionary. This is the way how you get arrays.now for dictionary

NSMutableDictionary *dict1 = [[NSMutableDictionary alloc]init];
   for(){
   [dict setObject:[dict objectforkey:@"id"] forKey:[dict objectForKey:@"procedure_name"];
   }

just check the for loop carefully in your code..


Abhi February 2016

i just get an easy way to create A:B type dictionary i am sharing my code i used and works perfectly for me

NSDictionary * searchdict = [NSDictionary dictionaryWithObjects:dataArray forKeys:iddict];
        NSLog(@"dict formed by combining %@",searchdict);

where dataarray and iddict are two arrays

it gives output dictionary as

1034 = "Catheterize for urine spec";
1035 = "Cauterization of cervix";
1036 = "CBC without platelet";
1037 = "CBC/diffwbc w/o platelet";
1038 = "Cbt 1st hour";
1039 = "Cbt each addl hour";
104 = "Amino acids quan 6 or more";
1040 = "Ccp antibody"; ......


PhaniRaghu February 2016

yaa, I know we can directly add two arrays to dictionary. But it takes memory for two arrays and dictionary also.. so instead of creating two arrays.you can directly add it to dictionary. But what you did is also correct. In mobile applications every time we have to think about memory management also..

Post Status

Asked in February 2016
Viewed 1,455 times
Voted 4
Answered 5 times

Search




Leave an answer