Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that contains movie objects. These objects are stored in a movie array. My movie object is below.

Movie.h

NSString * name;
NSString * cat_name;

I want to add my original array to a UITableView with dynamic rows and sections but I'm finding it difficult. I think the best way to do this is by having an array of arrays.

For example, there would be an array that contains all horror movies, an array that contains all fiction etc. All in one array. I think that would allow me to get the desired end product. I'm finding it difficult code it though.

EDIT The content of the array is dynamic, so I will not know how many objects will be in it at launch (it's being parsed from JSON). So I need to dynamically create the right amount of sections etc.

share|improve this question
    
What about having a dictionary movieDict = @{@"horror:horrorMoviesArray, @"fiction":fictionMoviesArray}; –  Anoop Vaidya Dec 8 '14 at 10:59
    
@AnoopVaidya yes this could work, an Array of arrays is what first came to mind. –  DevC Dec 8 '14 at 11:00
1  
@AnoopVaidya This is a good idea, but don't forget you'll also need an array of the dictionary keys in order to know which key is section 0, section 1, etc. –  Droppy Dec 8 '14 at 11:01
    
@Droppy Correct, getting it to work with a Tableview dynamically is the goal. –  DevC Dec 8 '14 at 11:03

1 Answer 1

up vote 3 down vote accepted
NSMutableDictionary * mainDictionary = [[NSMutableDictionary alloc] init];

Movie * firstHorrorMovie = [[Movie alloc] init];
firstHorrorMovie.name = @"Psycho";
firstHorrorMovie.cat_name = @"Horror";

Movie * secondHorrorMovie = [[Movie alloc] init];
secondHorrorMovie.name = @"Paranormal Activity";
secondHorrorMovie.cat_name = @"Horror";

Movie * comedyMovie = [[Movie alloc] init];
comedyMovie.name = @"The new guy";
comedyMovie.cat_name = @"Comedy";

NSArray * horrorMovies = [NSArray arrayWithObjects:firstHorrorMovie, secondHorrorMovie, nil];
NSArray * comedyMovies = [NSArray arrayWithObjects:comedyMovie, nil];

[mainDictionary setValue:horrorMovies forKey:@"Horror"];
[mainDictionary setValue:comedyMovies forKey:@"Comedy"];

OR (in your case - dynamically)

NSMutableDictionary * anotherMainDictionary = [[NSMutableDictionary alloc] init];

NSArray * array = [NSArray arrayWithObjects:firstHorrorMovie, secondHorrorMovie, comedyMovie, nil];
for (Movie * movie in array) {
    NSMutableArray * array = [anotherMainDictionary valueForKey:movie.cat_name];
    if (array) {
        [array addObject:movie];
        [anotherMainDictionary setValue:array forKey:movie.cat_name];
    } else {
        NSMutableArray * newArray = [NSMutableArray arrayWithObject:movie];
        [anotherMainDictionary setValue:newArray forKey:movie.cat_name];
    }
}
share|improve this answer
    
Why aren't you using the newer Objective-C literal syntax? –  Droppy Dec 8 '14 at 11:18
    
Because it is susceptible to crashes for null values –  Danut Pralea Dec 8 '14 at 11:21
    
No more than the code you show does... –  Droppy Dec 8 '14 at 11:21
1  
I just tested the code and it should work. I have updated my response. And yes, the dictionary will be empty at first, because you are going to populate it with the data that is coming from the server (your array in this case). If you still have problems with it, post more code on your side and I can adjust it in my response to fit your needs better –  Danut Pralea Dec 8 '14 at 11:55
1  
Thanks I got it working, there was an error in my own implementation. –  DevC Dec 8 '14 at 12:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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