---
title: "iPhone开发常用控件之表TableView 【二】，点击一行进行view跳转"
author: "android_zhang"
date: "2013-06-03T09:45:27Z"
tags: ["iOS"]
categories: ["移动开发"]
slug: "iphone-tableview-view"
draft: false
lastmod: "2013-06-03T09:45:27Z"
id: 1758705
url: "https://android-zhang.iteye.com/blog/1758705"
---

前面的多视图总结已经有过这个实现，现在把他放在这里，主要是练习一下TabView的使用。 Navigation通常与TableView搭配使用，博文iPhone开发常用控件之表TableView【一】 编写了一个TableView的示例，那是一个单视图应用，现在搭配上Navigation将其修改为多视图应用。 1，将TableView示例工程复制一份名称修改为TableViewDemo-Nav，打开该工程进行修改。 2、首先，修改AppDelegate，添加UINavigationController的实例，AppDelegate.h修改后如下： \[code="java"]#import @class ViewController; @interface AppDelegate : UIResponder @property (strong, nonatomic) UIWindow \*window; @property (strong, nonatomic) ViewController \*viewController; //添加navigationController @property (strong, nonatomic) UINavigationController \*navigationController; @end\[/code] 3,修改AppDelegate.m中的didFinishLaunchingWithOptions方法，如下： \[code="java"]- (BOOL)application:(UIApplication \*)application didFinishLaunchingWithOptions:(NSDictionary \*)launchOptions { self.window = \[\[\[UIWindow alloc] initWithFrame:\[\[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. self.viewController = \[\[\[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; //注释掉下面一行代码 /* self.window.rootViewController = self.viewController;\*/ //添加如下代码 self.navigationController = \[\[UINavigationController alloc]initWithRootViewController:self.viewController]; \[self.window addSubview:self.navigationController.view]; \[self.window makeKeyAndVisible]; return YES; }\[/code] 4、新建视图控制器AppViewController（带xib）,如下： \[img] \[img]http://dl.iteye.com/upload/attachment/0078/7666/92857965-c13d-3df1-bd26-3169d8b948cf.png\[/img] \[/img] 5、ViewController.h修改后如下： \[code="java"]#import #import "AppViewController.h" @interface ViewController : UIViewController @property(nonatomic,retain)NSMutableArray \*apps; @property(nonatomic,retain)AppViewController \*appViewController; @end\[/code] 6、ViewController.m中主要是实现了-(void)tableView:(UITableView\*)tableView didSelectRowAtIndexPath:(NSIndexPath \*)indexPath方法，如下： \[code="java"]//实现didSelectRowAtIndexPath -(void)tableView:(UITableView\*)tableView didSelectRowAtIndexPath:(NSIndexPath \*)indexPath{ NSString \*imageName = \[NSString stringWithFormat:@"%d",\[indexPath row]+1]; NSString \*appName = \[apps objectAtIndex:\[indexPath row]]; //初始化appViewController appViewController = \[\[AppViewController alloc]initWithNibName:@"AppViewController" bundle:nil]; //传递参数 appViewController.appName = appName; appViewController.appIconName = imageName; //跳转到appViewController \[self.navigationController pushViewController:appViewController animated:YES]; }\[/code] 7、AppViewController.h如下： \[code="java"]#import @interface AppViewController : UIViewController @property(strong,nonatomic)NSString \*appName; @property(strong,nonatomic)NSString \*appIconName; @property(strong,nonatomic)IBOutlet UILabel \*appNameLabel; @property(strong,nonatomic)IBOutlet UIImageView \*appIconImgView; @end \[/code] 8、注意：将输出口与AppViewController.xib中的UI控件相连。AppViewController.xib如下: \[img] \[img]http://dl.iteye.com/upload/attachment/0078/7668/d37d9745-e3f2-33c6-80d4-8a3ff69ec746.png\[/img] \[/img] 9、AppViewController.m如下： \[code="java"]#import "AppViewController.h" @interface AppViewController () @end @implementation AppViewController @synthesize appName; @synthesize appIconName; @synthesize appNameLabel; @synthesize appIconImgView; - (id)initWithNibName:(NSString \*)nibNameOrNil bundle:(NSBundle \*)nibBundleOrNil { self = \[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"AppViewController"; } return self; } - (void)viewDidLoad { \[super viewDidLoad]; // Do any additional setup after loading the view from its nib. self.appNameLabel.text = appName; self.appIconImgView.image = \[UIImage imageNamed:appIconName]; NSLog(@"appName=%@,appIconName=%@",appName,appIconName); } - (void)viewDidUnload { \[super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; appName = nil; appIconName = nil; appNameLabel = nil; appIconImgView = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end\[/code] 10、运行效果如下： \[img] \[img]http://dl.iteye.com/upload/attachment/0078/7670/9769a88e-5e58-3f72-84ac-460e3833137a.png\[/img] \[/img] \[img] \[img]http://dl.iteye.com/upload/attachment/0078/7672/882a8be4-4f49-3bf8-bf24-51e2b43184dd.png\[/img] \[/img]