我已經制作了簡單的可可觸摸應用程序,但是我從未使用UINavigationControllers,任何建議都將不勝感激。在基於標籤欄的應用程序上使用UINavigationController將數組添加到UITableView
我想向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
感謝您的快速回復。我試過你的方法,但UITableView仍然顯示空白單元格。我認爲這可能是與.xib文件中的連接有關的問題。我試圖張貼一些圖像,但因爲我是一個新成員,我不能。我會盡力解釋我能做的最好的: – robbiecutting
好吧,只要確保你已經在IB中設置了「數據源」和「委託」連接。 – mbm29414
感謝您的快速回復。我試過你的方法,但UITableView仍然顯示空白單元格。我認爲這可能是與.xib文件中的連接有關的問題。我試圖張貼一些圖像,但因爲我是一個新成員,我不能。我需要鏈接所有3個.xib文件嗎? TabBarController.xib,AtoZNavigationController.xib和AtoZTableController?我試過一些變化,但我不確定代碼或鏈接是否有錯誤。應用程序構建O.K和UITableView的標題可以在AtoZTableController.m文件中修改。再次感謝您的建議。 – robbiecutting