0

UITableView映画の予告編を表示するアプリを作成しています。

私のRootViewController.hファイルには次のコードがあります(必要な関連コードはすべてここに提供されています):

@interface RootViewController : UITableViewController {
    NSMutableArray *listOfLinks;
}

私の .m には次のコードがあります。

- (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];
}

私のplistは次のようになります:

<?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>

しかし、本質的にlistOfLinks空のエラーが発生しています。エラーは次のようになります。

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

NSLog配列を出力すると、それは文字列です。これの理由は何ですか?ご協力いただきありがとうございます!

4

3 に答える 3

2

配列に 1 つの辞書を追加し、6 番目の項目を取得しようとしていますが、1 つしかありません。ログに記録すると、そこにある単一の辞書の内容が表示されます。

numberOfRowsInSection を実行しているときは、おそらく配列ではなく辞書の数を返しているので、辞書が必要だと推測しているので、持っている必要があります

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

またはそれらの線に沿った何か

更新された質問の編集。

情報を辞書に保存している理由がわかりません。実際、辞書から要素の配列を取得し、それを使用して辞書を作成し、それを配列に入れています。それは意味がありません。

あなたが行くコードのあなたの部分で:

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

これは、その時点で配列を持っているように見えるので、なぜこれを行うのですか:

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

辞書を作成し、その辞書を配列の要素 0 に入れるのはどれですか?

MovieLinks キーから取得したリンクのリストを listOfLinks に含めたい場合は、それらを直接配列に入れてみませんか?

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

次に、アクセサーで:

- (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

テーブル ビューの numberOfRowsInSection メソッドを確認してください。このメソッドで返すもの。書きます

[listOfLinks カウント]
この方法で

于 2012-07-16T08:01:01.007 に答える
0

listOFLinks 内に NSDictionary を配置してから、NSURL を取得しようとしていますが、これは誤りです。

辞書内および配列内にある URL を取得する必要があります。

幸運を!

于 2012-07-16T08:01:12.040 に答える