ios - 在基于标签栏的应用程序上使用 UINavigationController 将数组添加到 UITableView

标签 ios xcode uitableview uinavigationcontroller tabbar

我以前制作过简单的 cocoa touch 应用程序,但我从未使用过 UINavigationControllers,任何建议将不胜感激。

我正在尝试将商店名称列表的数组添加到 UITableView。 UITableView 通过 UINavigation Controller 通过选项卡栏上的选项卡访问。

我有一个包含标签栏的 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

最佳答案

在您的 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;
}

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

试试这个:
-(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 中选择“选项卡式应用程序”模板,您会在您的应用程序委托(delegate)中获得以下方法(或多或少;我将其压缩了一点并“修复”了 Apple 使用点表示法的糟糕选择):

注:我相信您在推送新 View Controller 时遇到的问题现在将在下面得到解决... 尾注
- (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。

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

假设您要将 FirstViewController 保留为标准 UIViewController 子类,并且您将在其 View 中添加一个 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 的 View 上。
在 Interface Builder 中进行以下连接:
  • 右击File's Owner并将 TableView 属性连接到您放置在 FirstViewController View 上的 UITableView。
  • 右键单击 UITableView 并连接 两个 datasource 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];
    }
    

    关于ios - 在基于标签栏的应用程序上使用 UINavigationController 将数组添加到 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375770/

    相关文章:

    ios - 从 TableView 中删除时索引超出范围?

    TableView 单元格中的 iOS 布局约束冲突

    c# - Xamarin.IOS : UITableViewSource crashes on device

    ios - 我无法解决 "invalid nib registered for identifier (BNRItemCell)"

    objective-c - 即使使用默认 568h@2x 图像,iOS 应用程序也不会使用 4 英寸尺寸

    ios - 在 'tag' 类型的对象上找不到属性 '_strong id'

    ios - 我需要使用 4 英寸设备测试 3.5 英寸的 iOS 应用程序

    iOS 推送通知取决于网站内容

    ios - 检索 GKLeaderboard 中的所有分数

    ios - 以编程方式获取 WKInterfaceGroup 的背景颜色 (WatchKit)