0

これは私が以前に尋ねた質問に関連していますが、元の質問に答えました。新しい質問を開いても問題ないことを願っています。そうでない場合は、遠慮なく削除してください。

テーブルビューとタブバーを使用して、簡単なiPhoneアプリケーションを作成しようとしています。タイトルと表示する必要のあるデータの種類を除いて、すべてのビューはプログラムで同じです。それを除いて、それらはすべて同じように動作します。

現在、私のAppDelegateのコードは、ビューコントローラのさまざまなタブへの配布を処理し、それに応じてタイトルを設定します。ただし、私が理解できないのは、オブジェクトの特定の配列(すべての配列はタブごとに異なります)を各分散ビューコントローラーに渡して、テーブルビューで使用されるようにする方法です。

あなたが私を助けてくれることを願っています。私のコードは以下の通りです:

AppDelegate.h

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
  UIWindow *window;
  UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

AppDelegate.m

#import "RootViewController.h"
#import "MyAppDelegate.h"

@implementation MyAppDelegate.h

@synthesize window, tabBarController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
  NSDictionary *row1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"1", @"Id", 
                    @"My Name", @"Name", 
                    @"My Type", @"Type",
                    @"My Meta", @"Meta",
                    @"My Excerpt Text", @"Excerpt",
                    @"My Body Text", @"Body",
                    @"icon.jpg", @"Icon", 
                    @"image.jpg", @"Image", nil];

  NSArray *array = [[NSArray alloc] initWithObjects:row1, nil];
  self.events = array;

  NSArray *tableControllersConfig = [NSArray arrayWithObjects:
                                 [NSDictionary dictionaryWithObjectsAndKeys:@"test1", @"DataSource", @"Title of the Tableview", @"Title", @"icon.png", @"Icon", nil],

  NSMutableArray *tableControllers = [NSMutableArray array];
  for (NSDictionary *configDict in tableControllersConfig) {
    RootViewController *controller = [[RootViewController alloc] initWithNibName:@"TableView" bundle:nil];
    controller.tableView.delegate = controller;
    controller.title = [configDict valueForKey:@"Title"];
    controller.tabBarItem.image = [UIImage imageNamed: [configDict valueForKey:@"Icon"]];
    [tableControllers addObject:controller];
    [controller release];
  }

  self.tabBarController.viewControllers = [NSArray arrayWithArray:tableControllers];
  [window addSubview:tabBarController.view];
  [row1 release];
  [array release];
}

- (void)dealloc {
    [tabBarController release];
    [window release];
    [super dealloc];
}
@end

RootViewControllerは、UITableViewDataSourceプロトコルを実装します。

4

2 に答える 2

1

「RootViewController」がtableViewのデータソースである場合(UITableViewDataSourceプロトコルを実装します)

RootViewControllerクラスに「tableDataArray」と言うNSArrayivarを作成します。

このivarに保持される配列を含む新しいinitメソッドを作成します。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    // Custom initialization
    tableDataArray = [tableData retain];

}
return self;

}

tableViewのデータソースメソッドでtableDataArrayを使用します。

次に、ループ内にRootViewControllerを作成するときに、新しいinitメソッドを使用してtableDataArrayを設定し、各テーブルビューに異なる配列を設定できます。

お役に立てれば

于 2009-10-30T14:03:45.430 に答える
0

答えはあなたのデザインに依存します。

話しているデータが実行間で同じままである場合、そのデータはテーブルビューの基本構成の一部であるため、ViewControllerにパークする必要があります。

データが時間の経過とともに進化する場合は、データをアプリデリゲートに配置してから、そのデータへのポインターを持つカスタムビューコントローラーサブクラスを使用する必要があります。コアデータを使用するXcodeのテンプレートプロジェクトの1つを見てください。クラス間の関係は必要なものと同じですが、管理対象オブジェクトコンテキストの代わりに配列を使用します。

于 2009-10-30T16:10:55.687 に答える