1

I have this NSDictionary:

{
   Name =
   (
     "Lucas",
     "Tom"
  );
    Surname = 
  (
    "Jhon",
    "Smith"
    "Hop"
);

Name is a simple NSArray and Surname is a NSMutableArray.

Ok now my problem is this:

Starting from this dictionary, I must show in UITableView the key of each element of the NSDictionary. For example, in this case, UITableView will have two items, one call Name and the other Surname.

When I click on an element of the UITableView I will have to go to another controller that will show me all the data of the clicked cell. If for example, I click on Surname, when I open the new controller, I'll have to get it like Jhon, Smith and Hop.

To get started, I tried to show the UITableView before all the keys by writing:

self.KeyArray = [myDictionary allKeys];

and in the cellForRowAtIndexPath method:

cell.textLabel.text = [self.KeyArray objectAtIndex: indexPath.row];

But this is a wrong approach, because I'm going to lose all the information related to that specific key. Do you have any idea how I can do it?

Luca tom
  • 29
  • 7

3 Answers3

3

You can use delegate to send that NSMutableArray to another controller.

Lets say your first view controller is "A" which has those name and surname keys. And second Controller is "B" which will has the array that you want to send.
Now, 1. Create a protocol in A controller.
2. Create the delegate object of that protocol.
3. Create 1 Method in A controller which can have a parameter Like :- (NSMutableArray *).
4. Implement that protocol in to B Controller.
5. Declare the method in Controller B that you defined in Controller A
6. Call that method with the help of delegate object in didSelectRowAtIndexPath and Pass that Object in that method.
7. Now you have the NSMutableArray in that method and you are free to use that values.

Arpit Jain
  • 118
  • 4
2

You can do like that in didSelectRowAtIndexPath write below code

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *keyOfMainDic = [self.KeyArray objectAtIndex:indexPath.row]; // in the keyOfMainDic, you will get selected key name of your main dictionary
  NSMutableArray *temArray = [yourMaindict objectForKey:keyOfMainDic] // You will get array of specific key.

 // Here you just need to pass "temArray" to your destination controller.
}
iPatel
  • 46,010
  • 16
  • 115
  • 137
  • `Incompatible pointer types initializing 'NSMutableDictionary *' with an expression of type 'NSMutableArray * _Nullable'` Any idea? My dictionary is defined in this way: `NSMutableDictionary *> *dict;` – Luca tom Oct 13 '17 at 13:11
  • Answer is updated, Look at last like you need to take NSMutableArray not dictionary. – iPatel Oct 13 '17 at 13:16
  • You are my new god – Luca tom Oct 13 '17 at 13:18
1

First you should define a segue from your view controller that has tableview to your detail controller and give it an identifier in storyboard.

Then define a property into your detail controller header inherit from KeyArray type.

After that, just code it like below. This is just suggestion, please use a model for your array instead of dictionary.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

   self.KeyArrayItem = [self.KeyArray objectAtIndex:indexPath.row];
   [self performSegueWithIdentifier:@"yourSegueID" sender:self.KeyArrayItem];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    AnotherController *controller = segue.destinationViewController;
    controller.KeyArrayItem = sender;

}