0

私はナビゲーションベースのアプリケーションに取り組んでいます。私のrootViewControllerには3つのセルが含まれています-最初のセルが押されると、UIViewControllerがプッシュされます(これは機能します)-問題は、UITableViewControllerをプッシュするはずの2番目と3番目のセルにあります

アプリはエラーなしで実行されており、まったくクラッシュしていませんが、テーブルビューに移動すると、要素が含まれていない空のテーブルが表示されます。

コードのこの部分に問題がありますか?? :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    UIViewController *detailViewController = [[UIViewController alloc] initWithNibName:@"Introduction" bundle:nil];
    detailViewController.title=@"Introduction";

    UITableViewController *myPictures = [[UITableViewController alloc] initWithNibName:@"Pictures" bundle:nil];
    myPictures.title=@"Pictures";

    UITableViewController *myImages = [[UITableViewController alloc] initWithNibName:@"ImagesViewController" bundle:nil];
    myImages.title=@"Images";

    // Pass the selected object to the new view controller.
    if (0 == indexPath.row)
    [self.navigationController pushViewController:detailViewController animated:YES];

    if (1== indexPath.row)
    [self.navigationController pushViewController:myPictures animated:YES];

    if (2== indexPath.row)
    [self.navigationController pushViewController:myImages animated:YES];

    [detailViewController release];
    [myPictures release];   
    [myImages release];
4

1 に答える 1

0

あなたがしていることは非常に間違っています(元の問題は別として)。各View Controllerをインスタンス化し、現在の「セル選択」に基づいたものだけを使用するのはなぜですか? これにより、これらの個別のビューをそれぞれ読み込むのにかかる時間に応じて、アプリの速度が低下します。「if (indexPath.row == 2) {」ブロックで関連するView Controllerのみをインスタンス化する必要があります。

それとは別に、あなたのアプローチには多くの間違いがあります。汎用の UITableViewController をインスタンス化すると (独自の nib を指定した場合でも)、明らかに空のビューしか表示されないため、実行していることは決して機能しません。ニブがデリゲートとして関連付けられている独自のクラスが必要であり、これによりTableViewにデータが提供されます。

これらのnibファイル(「Pictures」など)を作成したときに、xcodeによって「PicturesViewController.hおよびPicturesViewController.m」ファイルも提供されると思いますか?その場合は、そこに適切なコードを記述し、「Pictures」でTableviewを確認する必要があります" nib ファイルの 'datasource' と 'delegate' は 'PicturesViewController' に設定されています。次に、そのビューを表示したい場合は、代わりに次のようにします。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.row == 1) {
        ...
    } else if (indexPath.row == 2) {
     PicturesViewController *myPictures = [[PicturesViewController alloc] initWithNibName:@"Pictures" bundle:nil];

     [self.navigationController pushViewController:myPictures animated:YES];
     [myPictures release];
    }

} 
于 2011-05-01T13:02:09.980 に答える