瞭解表視圖之後,我們瞭解導航控制器,導航控制器和表視圖沒有必然的關聯,但是在實際的運用當中他們是緊密相關的。
先了解一些基礎知識。或者說是複習吧。
什麼是棧?
棧是一種常用的數據結構,採用後進先出的原則。可以理解我們把書一本一本的裝到箱子裏面,當我們取的時候是把最後放進去的書最先拿出來。
向棧中添加對象叫入棧(push)即把對象推到棧中。
第一個入棧的對象叫基棧。
最有一個入棧的對象叫棧頂。
從棧中刪除對象的操作叫出棧(pop)。
這個導航項目由6個部分組成
1、展示按鈕視圖。2、校驗表視圖。3、行控制視圖。4、可移動行視圖。5、可刪除行視圖。6、可編輯詳細信息視圖。
接下來就創建項目。
創建的是以一個Empty Application創建的。
創建頂級視圖
首先要創建一個頂級類命名爲BIDFirstLevelController。然後在BIDAppDelegate頭文件中添加代碼如下:
#import <UIKit/UIKit.h>
@interface BIDAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) UINavigationController *navController;//初始化一個導航條
@end
#import "BIDAppDelegate.h"
#import "BIDFirstLevelController.h"
@implementation BIDAppDelegate
@synthesize window = _window;
@synthesize navController;//實例化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
BIDFirstLevelController *first=[[BIDFirstLevelController alloc]initWithStyle:UITableViewStylePlain];//這個方法可以用來創建一個普通的或時分組的的表視圖控制器,而不用藉助nib文件。
self.navController=[[UINavigationController alloc] initWithRootViewController:first];//傳遞頂層控制器,導航控制器用它來初始化內容
[self.window addSubview:navController.view];//它是由導航控制器所提供的符合視圖,有兩項組成,屏幕頂部的導航欄,以及下面所顯示的內容
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
其實這兩個文件是最基礎的初始化數據的時候必須加載的。所以導航條要添加在這裏,它第一次就要出現的。
接下來創建內容視圖的,BIDFirstLevelViewController,這個我們不會去創建實例,它單獨的存在,我們便可以向接下來編寫的其他控制器添加常規項目。可以理解爲是一個抽象類。
實現代碼如下:
#import <UIKit/UIKit.h>
@interface BIDFirstLevelController : UITableViewController
@property (strong,nonatomic) NSArray *controllers;
@end
#import "BIDFirstLevelController.h"
#import "BIDSecondLevelViewController.h"
@implementation BIDFirstLevelController
@synthesize controllers;
-(void) viewDidLoad
{
[super viewDidLoad];
self.title=@"First Level";
NSMutableArray *array=[[NSMutableArray alloc]init];//使用可變數組以迭代的方式添加新控制器。
self.controllers=array;
}
-(void) viewDidUnload
{
[super viewDidUnload];
self.controllers=nil;
}
#pragma mark -
#pragma mark Table Data Source Methods
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section//返回數組控制器中的計數
{
return [self.controllers count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *FirstLevelCell=@"FirstLevelCell";//充當表示某單元的鍵
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FirstLevelCell];
}
NSUInteger row=[indexPath row];//需要知道表視圖顯示那些行
BIDSecondLevelViewController *controller=[controllers objectAtIndex:row];
cell.textLabel.text=controller.title;
cell.imageView.image=controller.rowImage;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath//用戶單擊某行後調用的方法
{
NSUInteger row=[indexPath row];
BIDSecondLevelViewController *nextController =[self.controllers objectAtIndex:row];//從對應於該行的數組中獲取正確的控制器
[self.navigationController pushViewController:nextController animated:YES];//將下一個控制器從數組中取出,並放入到導航控制器棧中
}
@end
這樣就可以運行了,效果如下:這樣就是把導航條添加進去,由於數組爲空所以只能看到空行。
由於最近比較忙,這個學習還是精力不足,緩慢前進,總比不動好,逆水行舟不進則退。我要努力。。。。。。