0

I have a UITableView for an app I am creating that will show Movie Trailers.

In my RootViewController.h file I have this code (well all of the relevant code needed is supplied here):

@interface RootViewController : UITableViewController {
    NSMutableArray *listOfLinks;
}

In my .m is this code:

- (void)viewDidLoad {
    [super viewDidLoad];
    listOfLinks = [[NSMutableArray alloc] init];
    NSURL *myurl2 = [NSURL URLWithString:@"http://website.com/movietrailers.plist"];
    NSDictionary *plistDict2 = [NSDictionary dictionaryWithContentsOfURL:myurl2];
    NSArray *plistArray2 = [plistDict2 objectForKey:@"MovieLinks"];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:plistArray2 forKey:@"MovieLinks"];
    [listOfLinks addObject:dic];
    NSLog(@"%@", listOfLinks);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSURL *movieURL = [listOfLinks objectAtIndex:indexPath.row];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    [moviePlayer release];
}

My plist looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>MovieTitles</key>
    <array>
        <string>Movie 1</string>
        <string>Movie 2</string>
    </array>
    <key>MovieLinks</key>
    <array>
        <string>http://trailerserver.com/movie1.mp4</string>
        <string>http://trailerserver.com/movie2.mp4</string>
    </array>
</dict>
</plist>

But I am essentially getting the error that listOfLinks is empty. The error looks like this:

Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 0]'

When I NSLog the array it prints out it's strings. What is the reason for this? Thanks for your help!

4

3 回答 3

2

You added 1 dictionary to an array and are now trying to get the 6th item, you only have one. When you log it, you see the contents of the single dictionary you have there.

When you are doing the numberOfRowsInSection you are probably returning the count of the dictionary, not the array, and I am guessing you want the dictionary, so you should have

NSDictionary* d = [listOfLinks objectAtIndex:0];
NSURL *movieURL = [[listOfLinks [d allKeys]] objectAtIndex: indexPath.row];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL]

or something along those lines

EDIT for your updated question.

I am not sure why you are storing the info in a dictionary. In fact you are getting an array of elements from a dictionary, then creating a dictionary with it and putting it in an array. That makes no sense.

In your part of the code where you go:

NSDictionary *plistDict2 = [NSDictionary dictionaryWithContentsOfURL:myurl2];
NSArray *plistArray2 = [plistDict2 objectForKey:@"MovieLinks"];

this would appear you have an array at that moment, so why do you do this:

NSDictionary *dic = [NSDictionary dictionaryWithObject:plistArray2 forKey:@"MovieLinks"];
[listOfLinks addObject:dic];

which creates a dictionary and puts the dictionary in element 0 of an array?

If you want listOfLinks to contain a list of links that you get from the MovieLinks key, why not put them straight into the array?

if(plistArray2 != nil && [plistArray2 count] > 0)
    [listOfLinks addObjectsFromArray:plistArray2];

then in your accessors:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == WHATEVERSECTION)
        return [listOfLinks count];
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSURL *movieURL = [listOfLinks objectAtIndex:indexPath.row];
    MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    [self presentMoviePlayerViewControllerAnimated:moviePlayer];
    [moviePlayer release];
}
于 2012-07-16T08:01:38.480 回答
0

Check table view's numberOfRowsInSection method. What you are returning in this method. write

 [listOfLinks count] 
in this method

于 2012-07-16T08:01:01.007 回答
0

You are putting a NSDictionary inside the listOFLinks and then you are trying to retrieve a NSURL, that's wrong.

You need to get the URL that is inside the dictionary and inside the array.

Good luck!

于 2012-07-16T08:01:12.040 回答