0

这与我之前提出的一个问题有关,但原始问题已得到解答。希望可以打开一个新问题,如果没有,请随时删除它。

我正在尝试使用 tableviews 和 tabbar 构建一个简单的 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 类中创建一个 NSArray ivar 说“tableDataArray”,

创建一个新的 init 方法,该方法将包含一个要保留在此 ivar 中的数组:

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

答案取决于您的设计。

如果您正在谈论的数据从运行到运行保持不变,您应该将该数据停放在视图控制器中,因为它是表视图基本配置的一部分。

如果数据随着时间的推移而演变,那么您应该将其放在应用程序委托中,然后使用具有指向该数据的指针的自定义视图控制器子类。查看 Xcode 中使用核心数据的模板项目之一。类之间的关系与您需要的关系相同,但您将使用数组而不是托管对象上下文。

于 2009-10-30T16:10:55.687 回答