I’m new to iOS dev and I followed a tutorial that was a simple UITableview and a detail view.
This sets up my Array:
- (void)viewDidLoad
{
[self setupArray];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)setupArray
{
states = [[NSMutableDictionary alloc]init];
[states setObject:@"Thing 1" forKey:@"Subject 1"];
[states setObject:@"Thing 2" forKey:@"Subject 2"];
[states setObject:@"Thing 3" forKey:@"Subject 3"];
[states setObject:@"Thing 4" forKey:@"Subject 4"];
datasource = [states allKeys];
}
I have working cells and detail views. How do I add more objects to my keys? Is that possible? I need each subject [key] to have many attributes (i.e. a thing, a person, a place, a color)…
Can you break this down to the most simple terms for me? Thanks!
I’m not sure if I understand your question, but each key can have only one object associated with it. In your case, you’re using an NSString object. If you replaced the NSString with some object that you create, say AnObjectWithAThingAndAPersonAndAPlace, you could have multiple attributes associated with each key.
I think I understand what you want now. What you want is not an object with arrays associated to it, but an array of objects. You can do it with NSDictionary objects.
Then in your UITableViewDataSource method
and you can retrieve all the strings for that object.
If I were to do something like this, I would probably create a plist file containing the array. Then your setupArray method could look like this:
I though I would add a few more comments…In case it isn’t obvious, the objects you add to your dictionary don’t have to be NSStrings, they can be any object, such as an NSNumber, which may be useful for you in the case of your baseball players. Also, you may wish to create a custom player object instead of using an NSDictionary. And you may want to have something like a Core Data database where the players are stored and retrieved (instead of hard coding them or getting them from a plist file). I hope my answer can get you started on the right path though.