1

我正在使用 XCode 4.2 并在 iPad 5.0 上测试我的构建。

我开始使用 XCode 中的标准选项卡式应用程序构建一个应用程序,然后添加代码以在第一个选项卡内有 2 个 uitableviews。

它可以编译,但表数据不会加载到视图中。

应用委托.h:

@interface dmbAppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UITabBarController *tabBarController;

@end

AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    UIViewController *viewController1 = [[[dmbFirstViewController alloc] initWithNibName:@"dmbFirstViewController" bundle:nil] autorelease];
    UIViewController *viewController2 = [[[dmbSecondViewController alloc] initWithNibName:@"dmbSecondViewController" bundle:nil] autorelease];
    self.tabBarController = [[[UITabBarController alloc] init] autorelease];
    self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
    NSLog(@"Loading first tab view from app delegate...");
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];
    return YES;
}

FirstViewController.h:

@interface dmbFirstViewController : UIViewController {

    ReservationsTable *reservationsController;
    WaitlistTable *waitlistController;
    IBOutlet UITableView *reserveTable;
    IBOutlet UITableView *waitlistTable;
}

FirstViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSLog(@"FirstView Controller - View Loading Started");

    [reserveTable setDataSource:reservationsController];
    [waitlistTable setDataSource:waitlistController];

    NSLog(@"FirstView Controller - Loading Table Views..");

    [reserveTable setDelegate:reservationsController];
    [waitlistTable setDelegate:waitlistController];
    reservationsController.view = reservationsController.tableView;
    waitlistController.view = waitlistController.tableView;

    NSLog(@"FirstView Controller - View Loading Finished");
}

这两个表都有一个 .h 和 .m 并实现了标准表方法。我还在第一个视图 nib 文件中添加了 2 个表并将它们链接到文件所有者。

更新: ReserveTable.h:

    @interface WaitlistTable : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
        NSMutableArray *waitlistitems;
    }

ReserveTable.m:

- (void)viewDidLoad
{
    NSLog(@"View Did Load - Wait List Table");
    waitlistitems = [[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"6",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",nil] retain];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    NSLog(@"Inside number of section for Wait List table...");
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"Inside numberofRows for Wait List table...");
    // Return the number of rows in the section.
    return [waitlistitems count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Inside cell for row at index path for Wait List table...");
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    cell.textLabel.text = [NSString stringWithFormat:@"1.%@" ,[waitlistitems objectAtIndex:indexPath.row]];    
    return cell;
}

想法?

4

2 回答 2

1

我认为在你的firstViewController你应该推动意见。假设那reserveTable是你的主表,像这样推它

[self.view addSubView:reserveTable];

另外,您是否ReserveTable.hfirstViewController?

如果是,您应该在那里初始化表。

但是我的建议是转换firstViewControllerin a tableViewController,编辑App Delegate 和 thefirstViewController.h和 m,然后从那里初始化表(甚至添加另一个表)。这样会更容易!

于 2012-06-25T19:58:53.617 回答
1

将您更改viewDidLoad为:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    reservationsController = [[ReservationsTable alloc] init];
    waitlistController = [[WaitlistTable alloc] init];

    NSLog(@"FirstView Controller - View Loading Started");

    [reserveTable setDataSource:reservationsController];
    [waitlistTable setDataSource:waitlistController];

    NSLog(@"FirstView Controller - Loading Table Views..");

    [reserveTable setDelegate:reservationsController];
    [waitlistTable setDelegate:waitlistController];

    NSLog(@"FirstView Controller - View Loading Finished");
 }

基本上,你永远不会初始化你的tableViewControllers(我猜这是他们两个的名字,我会把他们的名字改成“WaitlistTableViewController”和“ReservationsTableViewController”,但这只是我。)另外,将“tableView”设置为“视图”是不必要的。

或者更好的是,在您的dmbFirstViewController.

或者dmbFirstViewController像这样使用:

dmbFirstViewController.h:

@interface dmbFirstViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {

    ReservationsTable *reservationsController;
    WaitlistTable *waitlistController;
    IBOutlet UITableView *reserveTable;
    IBOutlet UITableView *waitlistTable;
    NSMutableArray *waitlistitems;
    NSMutableArray *reserveitems;
}

dmbFirstViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.


    waitlistitems = [[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"6",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",nil] retain];

    NSLog(@"FirstView Controller - View Loading Started");

    [reserveTable setDataSource:self];
    [waitlistTable setDataSource:self];

    NSLog(@"FirstView Controller - Loading Table Views..");

    [reserveTable setDelegate:self];
    [waitlistTable setDelegate:self];

    NSLog(@"FirstView Controller - View Loading Finished");
}

- (void)viewDidAppear:(BOOL)animated
{
    [reserveTable reloadData];
    [waitlistTable reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    NSLog(@"Inside number of section for Wait List table...");
    if(tableView == waitlistTable)
    {
        //Return sections for waitlistTable
        return 1;
    }else{
        //Return sections for reservedTable
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==waitlistTable)
    {
        NSLog(@"Inside numberofRows for Wait List table...");
        // Return the number of rows in waitlistTable section.
        return [waitlistitems count];
    }else{
        // Return the number of rows in reservedTable section.
        return [reserveditems count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(tableView == waitlistTable)
    { 
        NSLog(@"Inside cell for row at index path for Wait List table...");
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }

        // Configure the cell...
        cell.textLabel.text = [NSString stringWithFormat:@"1.%@" ,[waitlistitems objectAtIndex:indexPath.row]];    
        return cell;
    } else {
        //Create cell for reservedTable Cell
        .....
        return cell;
    }
}

你必须完成关于reservedTable细胞的部分,我没有那个代码。另外,我猜到了 items 数组reservedTable并没有对其进行初始化。

于 2012-06-25T20:04:50.690 回答