2

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!

3 Answers 3

3

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.

- (void)setupArray
{
    NSMutableArray *objectArray = [[NSMutableArray alloc] init];

    NSMutableDictionary *object1 = [[NSMutableDictionary alloc] init];
    [object1 setObject:@"Apple" forKey:@"thing"];
    [object1 setObject:@"Alex" forKey:@"person"];
    [object1 setObject:@"Alabama" forKey:@"place"];
    [object1 setObject:@"Azure" forKey:@"color"];
    [objectArray addObject:object1];

    NSMutableDictionary *object2 = [[NSMutableDictionary alloc] init];
    [object2 setObject:@"Banana" forKey:@"thing"];
    [object2 setObject:@"Bill" forKey:@"person"];
    [object2 setObject:@"Boston" forKey:@"place"];
    [object2 setObject:@"Blue" forKey:@"color"];
    [objectArray addObject:object2];

    datasource = [NSArray arrayWithArray:objectArray];
}

Then in your UITableViewDataSource method

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger row = [indexPath row];        
    NSDictionary *object = [datasouce objectAtIndex:row];

    ...

}

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:

- (void)setupArray
{
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"YourFileName" ofType:@"plist"];
    NSDictionary *plistData = [NSDictionary dictionaryWithContentsOfFile:filePath]; 
    datasource = (NSArray*)[plistData objectForKey:@"ObjectsForTableView"];
}

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Okay! So how do I create an object with arrays attached to it? For example, I need a list of players in my UITableview, then each player needs their height, weight, and batting average passed to the detail view controller--maybe an image too. How do I get all those things associated with the UITableViewCell?
I think I see what you want to do now. I will update my answer.
This is looking good. Can you help me out just a bit more? How do I label the cells? This doesn't work: cell.textLabel.text = [object objectForKey:@"person"]; How do I access the strings from the detail view controller? Like I said, I'm way new. This is a huge help! :)
Hmm...that should work. A few questions; 1 - I assumed that you were using ARC so there is no memory management here. You are using ARC right? 2 - Have you set your view controller as the table view's data source in Interface Builder? 3 - When you say it doesn't work, do you mean that the label doesn't appear, or is there a crash?
You could also see if [object objectForKey:@"person"] is returning a value. If it is returning a string and you're not seeing it in your table view, or if the data source method isn't even being called, then you probably haven't set the view controller as the table view's data source.
|
0

RIght now your datasource object is an NSArray. You need to make it an NSMutableArray. Declare it as an NSMutableArray in your header file and then you can do this:

datasource = [[states allKeys] mutableCopy];
[datasource addObject:whatever];

But, it sounds like the structure you are actually looking for is an NSMutableArray of NSDictionary objects. Like this:

NSDictionary *item = [NSDictionary dictionaryWithObjectsAndKeys:@"object1", @"key1", @"object2", @"key2", nil];
[datasource addObject:item]

;

1 Comment

Thanks, Michael! This is a big help. Could you give me some more detail/example? I'm very new and this only makes a little sense to me.
0

There are numerous ways (better than your given example) to do this. But I'll follow your example.

You are assigning an NSString Object to your keys.

What you can do is create a class Thing that contains all your attributes. and assign an instance of that class to your keys. ie.

[states setObject:myThingObject forKey:@"Subject 4"];

Then pass the myThingObject to your Detail View.

UPDATE:

Thing class contains the following properties:

  - person
  - place
  - color
  - thingName

So,

[states setObject:@"Thing 1" forKey:@"Subject 1"];

becomes

[states setObject:firstObject forKey:@"Subject 1"];
[states setObject:secondObject forKey:@"Subject 2"];

Note firstObject & secondObject are instances of your Thing class

To read more about classes in Objective-c visit:

https://developer.apple.com/library/ios/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/_index.html

1 Comment

Like I said, I'm new to iOS and programming in general. Can you break this down a little more? How do I add more objects to the key? (if that's even correct terminology).

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.