1

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.

4
  • What about having a dictionary movieDict = @{@"horror:horrorMoviesArray, @"fiction":fictionMoviesArray}; Commented Dec 8, 2014 at 10:59
  • @AnoopVaidya yes this could work, an Array of arrays is what first came to mind. Commented Dec 8, 2014 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. Commented Dec 8, 2014 at 11:01
  • @Droppy Correct, getting it to work with a Tableview dynamically is the goal. Commented Dec 8, 2014 at 11:03

1 Answer 1

3
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];
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Why aren't you using the newer Objective-C literal syntax?
Because it is susceptible to crashes for null values
In the second solution where is the main dictionary coming from?
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
Thanks I got it working, there was an error in my own implementation.
|

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.