Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Dictionary retrieval of value for key IOS

vnhrmthvnhrmth Posts: 4New Users
edited August 2012 in iOS SDK Development
This is just a beginner restaurant menu

there is a tableview containing items and price and when i click one item,it displays another view where the user has to input the quantity and click a done button,so when the user clicks done i want to multiply the quantity times the price,how do i retrieve that particular price and multiply it with the quantity user input in the textfield

Here's my code

I have declared NSDictionary in the Menu header file called

NSDictionary *dict;

my viewdidload method

 dict=[[NSDictionaryalloc]initWithObjectsAndKeys:
                         @"TomatoSoup",@"20.00",@"VegManchowSoup",@"12.00",nil];
           NSLog(@"%@",dict);
           [super viewDidLoad];
i have displayed this contents in a table view

 - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:  (NSInteger)section
           {
          return [[dict allKeys]count];
              }


           - (UITableViewCell *)tableView:(UITableView *)tableView      cellForRowAtIndexPath:(NSIndexPath *)indexPath
            { 
               static NSString *CellIdentifier = @"Cell";

                 UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:CellIdentifier];
                 if (cell == nil) {
                   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

NSArray *sortedkeys=[[dict allKeys]sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *key=[sortedkeys objectAtIndex:indexPath.row];
NSString *value=[dict objectForKey:key];
cell.textLabel.text=value;
cell.detailTextLabel.text=key;
return cell;
 }

     - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
          {
           if(indexPath.row==0)
            {   

         VegQuantity *vegetarian1 = [[VegQuantity alloc]   initWithNibName:@"VegQuantity" bundle:nil];
    vegetarian1.m_SelectedIndexPath=indexPath.row;
    vegetarian1.pass=dict;
    [self presentModalViewController:vegetarian1 animated:YES];
}
if(indexPath.row==1)
{   

    VegQuantity *vegetarian1 = [[VegQuantity alloc] initWithNibName:@"VegQuantity" bundle:nil];
    vegetarian1.m_SelectedIndexPath=indexPath.row;
    [self presentModalViewController:vegetarian1 animated:YES];
}
}
VegQuantity.h There is a View that has a textfield and a button saying done, now when i click the done button i need to retrieve the value for that particular soup and multiply it with the number of quantity i input

my problem is how am i supposed to retrieve the price(value) for that particular key and multiply it with the quantity

Replies

  • Duncan CDuncan C Posts: 9,116Tutorial Authors, Registered Users @ @ @ @ @ @ @
    You have not provided a complete picture of the problem.

    I gather that your dictionary is where you are storing the data for your table view?

    That is a bad idea. Dictionaries are unordered collections. Your cellForRowAtIndexPath method is getting a list of keys from your dictionary and sorting it every time you need to find a cell.

    That's bad for a number of reasons. First, sorting is pretty slow on anything but VERY small lists. Re-sorting the array for every cell is a dreadfully inefficient way to do things.

    Next, sorting doesn't always yield the same order of objects. If two sort keys are the same, the ordering of those keys is undefined. A table view will get messed up if the order of the data in the cells changes between calls to cellForRowAtIndexPath.

    You should restructure your data model to save the data as an array of objects, not a dictionary. You can use an array of dictionaries, an array of data objects, or whatever you want, but you need to use an ordered collection to save the data for a table view.

    Once you've done that, when the user taps on a particular cell, you can get the row number of that cell, use it to fetch the data for that item, and pass it to your modal view controller. Note that you should really be using the newer methods presentViewControllerAnimated:completion: and dismissViewControllerAnimated:completion:

    You can set up your modal view controller with a delegate property that points to the parent view controller containing the table view.

    The action on the done button for your modal view controller can then call a method in the delegate that passes back the count the user entered (and the total amount, if you want the modal view controller to calculate it rather than the parent view controller.)
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • vnhrmthvnhrmth Posts: 4New Users
    Hey thanks i figured it out..:-)
Sign In or Register to comment.