0

Hello and thank you for your time. Im fairly new to IOS/Objective C.

I have multiple variables setup globally at the top of my viewcontroller.

NSMutableArray * A;
NSMutableArray * B;
NSMutableArray * C;

Now when someone selects a cell in a tableview I would like to use the name of that cell to select one of the global variables. I found something for doing this with viewcontrollers, but I need something for misc variables as well. I am reffering to:

id myNewController = [[NSClassFromString(selected) alloc] init];
[[self navigationController] pushViewController:myNewController animated:YES];

So it would be something like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Save text of the selected cell:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
stringVariable = cell.textLabel.text;// value=A,B or C

// match string to array and load that array.
??         

}

Thanks in advance for any help, and sorry if this has been asked but I couldn't find anything that worked so as a last resort im asking for help :)

1 Answer 1

1

You may be better off storing your arrays as keys on a dictionary, instead of as individual fields. For example:

NSDictionary* dictionary;

dictionary = @{
    @"A": [[NSMutableArray alloc] init],
    @"B": [[NSMutableArray alloc] init],
    @"C": [[NSMutableArray alloc] init]
};

Then when your row is selected, you could lookup the array by key:

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString* key = cell.textLabel.text;// value=A,B or C

NSMutableArray* array = dictionary[key];
....
2
  • Seems like a viable solution. Ive seen that before I just needed it simplified so I could grasp it. I'm going to try and impliment it and if it meets my expectations I will rate you up. Thanks for the help bendytree :)
    – Anthem127
    May 29, 2013 at 16:25
  • This led me down the right path, the only thing I needed to do was change the way I was initializing the dictionary. I needed to add existing arrays to the dictionary, and then pass them to the event handler like so dictionary= [NSDictionary dictionaryWithObject:A forKey:@"A"]; that did the trick fr me, thanks!
    – Anthem127
    May 29, 2013 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.