【问题标题】:Adding an Array to a UITableView, using a UINavigationController on a Tab Bar based application在基于标签栏的应用程序上使用 UINavigationController 将数组添加到 UITableView
【发布时间】:2012-05-09 16:27:15
【问题描述】:

我以前做过简单的可可触摸应用,但我从未使用过 UINavigationControllers,任何建议将不胜感激。

我正在尝试将商店名称列表的数组添加到 UITableView。 UITableView 通过 UINavigation 控制器通过标签栏上的标签访问。

我有一个包含标签栏的 TabBarController.xib 文件。 我还有一个包含 UINavigationController 的 AtoZNavigationController.xib。 我有一个包含 UITableView 的 AtoZTableController.xib 文件。

这是我的 AppDelegate.h:

#import <UIKit/UIKit.h>
@class AtoZNavigationController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) IBOutlet UITabBarController *rootController;
@property (strong, nonatomic) IBOutlet AtoZNavigationController *navController;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "AtoZNavigationController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize rootController;
@synthesize navController;

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:       (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

[[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil];
  [self.window addSubview:rootController.view];
  self.window.backgroundColor = [UIColor whiteColor];
  [self.window makeKeyAndVisible];

return YES;
}
@end

AtoZNavigationController.h

#import <UIKit/UIKit.h>

@interface AtoZNavigationController : UINavigationController

@end

AtoZNavigationController.m

#import "AtoZNavigationController.h"

@interface AtoZNavigationController ()

@end

@implementation AtoZNavigationController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

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

-(void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

AtoZTableController.h

#import <UIKit/UIKit.h>

@interface AtoZTableController : UITableViewController <UITableViewDelegate,       UITableViewDataSource>
{
    IBOutlet UITableView *AtoZTableView;
    NSMutableArray *AtoZArray;
}

@property (nonatomic, retain) IBOutlet UITableView *AtoZTableView;
@end

AtoZTableController.h

#import "AtoZTableController.h"

@interface AtoZTableController ()

@end

@implementation AtoZTableController
@synthesize AtoZTableView;

-(id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

 -(void)viewDidLoad
 {
     [super viewDidLoad];

     self.title = NSLocalizedString(@"A to Z", @"An A to Z List of Stores");

     AtoZArray = [[NSMutableArray alloc] init];

    [AtoZArray addObject:@"Apple"];
    [AtoZArray addObject:@"Boots"];
    [AtoZArray addObject:@"Topman"];
}

 -(void)viewDidUnload
{
     [super viewDidUnload];
     // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 0;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [AtoZArray count];
}

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...

    NSInteger row = [indexPath row];
    cell.textLabel.text = [AtoZArray objectAtIndex:row];
    return cell;
}
#pragma mark - Table view delegate

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}
@end

【问题讨论】:

    标签: ios xcode uitableview uinavigationcontroller tabbar


    【解决方案1】:

    在你的AtoZTableController.h,你有问题。

    问题出在您的“tableView:cellForRowAtIndexPath:”方法中。 这是你所拥有的:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        // Configure the cell...
    
        NSInteger row = [indexPath row];
        cell.textLabel.text = [AtoZArray objectAtIndex:row];
        return cell;
    }
    

    问题是您永远不会处理来自dequeueReusableCellWithIdentifier:CellIdentifier 的返回值nil

    试试这个:

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
        // What happens if you don't get a cell to use?
        // This is the way to create a new, default UITableViewCell
        if (!cell) {            
            // You can look at the UITableViewCell class reference to see the 4 available styles
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
    
        // Configure the cell...
        NSInteger row = [indexPath row];
        cell.textLabel.text = [AtoZArray objectAtIndex:row];
        return cell;
    }
    

    编辑/更新:

    好的,所以要确切地知道你的错误在哪里有点困难,所以我会为你设置/描述一个典型的情况(或者我会怎么做)。

    如果您创建一个新应用并在 Xcode 中选择“选项卡式应用程序”模板,您会在您的应用程序委托中获得以下方法(或多或少;我对其进行了一点压缩并“修复”了 Apple 使用 dot 的错误选择符号):

    注意:我相信您在推送新视图控制器时遇到的问题现在将在下面得到解决...结束说明

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        [self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
        // Override point for customization after application launch.
        UIViewController *vc1 = [[FirstViewController alloc] initWithNibName:@"FirstVC" bundle:nil];
        // New line...
        UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:vc1];
        UIViewController *vc2 = [[SecondViewController alloc] initWithNibName:@"SecondVC" bundle:nil];
        [[self setTabBarController:[[UITabBarController alloc] init]];
        // Change here, too...
        [[self tabBarController] setViewControllers:[NSArray arrayWithObjects:navC, vc2, nil]];
        [[self window] setRootViewController:[self tabBarController]];
        [[self window] makeKeyAndVisible];
        return YES;
    }
    

    此方法设置了启动应用所需的一切,其中创建了 2 个 UIViewController,并将其设置为 UITabBarController 内的选项卡 1 和选项卡 2。

    现在,您可以使 FirstViewController 和 SecondViewController 成为您想要的任何东西。出于这个问题的目的,我们假设您要更改 FirstViewController 以托管 UITableView,当用户选择屏幕上的一行时,它将推送一个详细的 UIViewController。

    要求 EITHER FirstViewController 必须是 UITableViewController 的子类(这不是默认模板提供的)或者您必须将 UITableView 添加到 FirstViewController 的视图并设置关闭所有连接。

    假设您要将 FirstViewController 保留为标准 UIViewController 子类,并且您将在其视图中添加一个 UITableView。 (我可能会将其更改为 UITableViewController 子类,但这可能会更令人困惑。)

    首先,在 FirstViewController.h 中修改:

    @interface MMFirstViewController : UIViewController 
    @end
    

    到这里:

    @interface MMFirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
        UITableView *TableView;
    }
    @property (strong, nonatomic) IBOutlet UITableView *TableView;
    @end
    

    接下来,在 FirstViewController.m 中,合成 TableView 属性 (@synthesize TableView)。 接下来,单击 Xcode 中的 FirstViewController.xib 以在 Interface Builder 中加载它(我假设您使用的是 Xcode 4)。 现在,将 UITableView 从控制面板拖到 UIViewController 的视图上。 在 Interface Builder 中进行以下连接:

    1. 右键单击File's Owner,将TableView 属性连接到您在FirstViewController 视图中放置的UITableView。
    2. 右键单击 UITableView 并将 BOTH datasource AND delegate 属性连接到 File's Owner

    现在,您发布的初始化和填充AtoZArray 的代码应该可以正常工作。不要忘记复制您之前拥有的 3 个 UITableView 方法,numberOfSectionsInTableView:tableView:numberOfRowsInSection:tableView:cellForRowAtIndexPath:

    这些步骤应该可以帮助您工作,还应该让您了解您在设置中可能出错的地方。请注意,您仍然需要自己找出tableView:didSelectRowAtIndexPath: 才能推入您的新 UIViewController。

    这是一个让您入门的预告片:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        // Navigation logic may go here. Create and push another view controller.
        ThirdViewController *detailVC = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
        [[self navigationController] pushViewController:detailVC animated:YES];
    }
    

    【讨论】:

    • 感谢您的快速回复。我试过你的方法,但 UITableView 仍然显示空白单元格。我认为这可能是我在 .xib 文件中的连接问题。我试图发布一些图片,但由于我是新成员,我不能。我会尽力解释:
    • 好吧,只要确保你已经在 IB 中建立了 data sourcedelegate 连接。
    • 感谢您的快速回复。我试过你的方法,但 UITableView 仍然显示空白单元格。我认为这可能是我在 .xib 文件中的连接问题。我试图发布一些图片,但由于我是新成员,我不能。我需要所有 3 个 .xib 文件中的链接吗? TabBarController.xib、AtoZNavigationController.xib 和 AtoZTableController?我尝试了一些变化,但我不确定代码或链接中是否有错误。应用构建成功,UITableView 的标题可以在 AtoZTableController.m 文件中修改。再次感谢您的建议。
    • 谢谢。终于设法让它工作,我没有做的一件事是指定部分的数量,我将它设置为 0。我认为我添加的每个新页面都是一个新部分是否正确?再次感谢您的帮助.现在只是为了把它变成我原来的......
    • 另外,这种方法确实让我更好地理解了标签栏是如何以编程方式创建的。再次感谢伙计,真的很感激。
    【解决方案2】:

    我必须创建一个navigationController 的实例并将它与tabBarController 一起传递到我的appDelegate.m 中的子视图中。以后可以在我的 UITableViewController 中引用它。

    这是我添加的代码:

    navigationController = [[UINavigationController alloc] initWithRootViewController:_tabBarController];
    [self.window addSubview:_tabBarController.view];
    [self.window addSubview:navigationController.view];
    [self.window makeKeyAndVisible]; 
    

    navigationController 只是 UITableViewController 或 UIViewController 子类的一个实例,具体取决于您要显示的屏幕类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      相关资源
      最近更新 更多