Kevin Brown
Kevin Brown

Reputation: 12650

UITableview and datasource as NSMutableArray

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!

Upvotes: 2

Views: 3276

Answers (3)

Darren
Darren

Reputation: 10129

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.

Upvotes: 3

Mohamed Ibrahim
Mohamed Ibrahim

Reputation: 190

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

Upvotes: 0

Michael Frederick
Michael Frederick

Reputation: 16714

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]

;

Upvotes: 0

Related Questions