一、首先我们先创建一个iphone或ipad工程(本例以iphone为例)命名TableViewDemo1
如下图所示:
二、打开TableViewDemo1ViewController.xib,添加TableView控件。
三、编辑TableViewDemo1ViewController.h
添加实现的协议UITableViewDelegate,UITableViewDataSource,及声明UITableView对象tableViewList
- @interface TableViewDemo1ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
- IBOutlet UITableView *tableViewList;
- }
- @end
@interface TableViewDemo1ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { IBOutlet UITableView *tableViewList; } @end
四、打开TableViewDemo1ViewController.xib,IB设计器使TableView控件与之前声明的对象tableViewList做关联。
打开以上窗口,右键选中File's Owner并拖动至Table View上
在弹出菜单中选tableViewList
然后再右键选中Table View拖至File's Owner,淡出菜单如下
分别选中dataSource和delegate
至此IB设计完毕,下一步我们会在类中添加导航的实现代码。
五、添加实现代码
打开编辑TableViewDemo1ViewController.h
添加 NSMutableArray *dataItems;
- @interface TableViewDemo1ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> {
- IBOutlet UITableView *tableViewList;
- NSMutableArray *dataItems;
- }
- @end
@interface TableViewDemo1ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource> { IBOutlet UITableView *tableViewList; NSMutableArray *dataItems; } @end
打开编辑TableViewDemo1ViewController.m
在viewDidLoad中初始化dataItems
- - (void)viewDidLoad {
- [super viewDidLoad];
- dataItems=[[NSMutableArray alloc]initWithObjects:@"中国",@"美国",@"日本",nil];
- }
- (void)viewDidLoad { [super viewDidLoad]; dataItems=[[NSMutableArray alloc]initWithObjects:@"中国",@"美国",@"日本",nil]; }
添加数据源, 由三个函数来回答数据绑定的请求:numberOfSectionsInTableView, numberOfRowsInSection 和 cellForRowAtIndexPath.
用numberOfSectionsInTableView方法来返回table中有几个组.
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- {
- return 1;
- }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; }
用numberOfRowsInSection方法来返回每个组里有几行
- - (NSInteger)tableView:(UITableView *)tableView
- numberOfRowsInSection:(NSInteger)section
- {
- return [dataItems count];
- }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [dataItems count]; }
最后用cellForRowAtIndexPath来得到一个包含每一行显示信息的UITableViewCell对象. UITableViewCell类支持文本和图像,编辑和删除确认等功能. 这些信息会保存在表队列里,用来至此翻页等功能,但是内存很低的时候会自动释放,然后再需要的时候重新创建.
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
- }
- NSUInteger row=[indexPath row];
- cell.textLabel.text=[dataItems objectAtIndex:row];
- return cell;
- }
// Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSUInteger row=[indexPath row]; cell.textLabel.text=[dataItems objectAtIndex:row]; return cell; }
OK至此最基本的导航菜单我们算是完成了,运行一下看看效果
TableViewDemo1.zip (674.1 KB)
一、首先先丰富一下导航列表
目标:1、加上图标;2、加上明细;3、加上导航按钮;
准备三个图标文件并拖拽到工程下的Resources下
在h文件中添加图标NSMutableArray *iconItems; NSMutableArray *detailItems;声明代码
@interface TableViewDemo1ViewController :
- UIViewController<UITableViewDelegate,UITableViewDataSource> {
- IBOutlet UITableView *tableViewList;
- NSMutableArray *dataItems;
- NSMutableArray *iconItems;
- NSMutableArray *detailItems;
- }
- @end
UIViewController<UITableViewDelegate,UITableViewDataSource> { IBOutlet UITableView *tableViewList; NSMutableArray *dataItems; NSMutableArray *iconItems; NSMutableArray *detailItems; } @end
在m文件viewDidLoad添加iconItems的初始化代码
- - (void)viewDidLoad {
- [super viewDidLoad];
- dataItems=[[NSMutableArray alloc]initWithObjects:@"中国",@"美国",@"日本",nil];
- iconItems=[[NSMutableArray alloc]initWithObjects:@"cn",@"us",@"jp",nil];
- detailItems=[[NSMutableArray alloc]initWithObjects:@"China",@"America",@"Japan",nil];
- }
- (void)viewDidLoad { [super viewDidLoad]; dataItems=[[NSMutableArray alloc]initWithObjects:@"中国",@"美国",@"日本",nil]; iconItems=[[NSMutableArray alloc]initWithObjects:@"cn",@"us",@"jp",nil]; detailItems=[[NSMutableArray alloc]initWithObjects:@"China",@"America",@"Japan",nil]; }
以上都准备好后修改UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法
注意实例化cell时注意样式由原来的UITableViewCellStyleDefault缺省改成UITableViewCellStyleSubtitle
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- //实例化cell时注意样式由原来的UITableViewCellStyleDefault缺省改成UITableViewCellStyleSubtitle
- cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
- }
- NSUInteger row=[indexPath row];
- //添加图标
- cell.imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[iconItems objectAtIndex:row]]];
- cell.textLabel.text=[dataItems objectAtIndex:row];
- //添加明细
- cell.detailTextLabel.text =[detailItems objectAtIndex:row];
- // Configure the cell.
- return cell;
- }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //实例化cell时注意样式由原来的UITableViewCellStyleDefault缺省改成UITableViewCellStyleSubtitle cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSUInteger row=[indexPath row]; //添加图标 cell.imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[iconItems objectAtIndex:row]]]; cell.textLabel.text=[dataItems objectAtIndex:row]; //添加明细 cell.detailTextLabel.text =[detailItems objectAtIndex:row]; // Configure the cell. return cell; }
添加导航按钮 在m文件中添加委托函数– tableView:accessoryTypeForRowWithIndexPath:
- -(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
- //返回类型选择按钮
- return UITableViewCellAccessoryDetailDisclosureButton;
- }
-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{ //返回类型选择按钮 return UITableViewCellAccessoryDetailDisclosureButton; }
好现在我们执行程序看看
但是需要注意的时官方文档告诉我们accessoryTypeForRowWithIndexPath这个委托函数在iOS 3.0之后已经被废弃了,现在可以在UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath方法中添加
cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- //实例化cell时注意样式由原来的UITableViewCellStyleDefault缺省改成UITableViewCellStyleSubtitle
- cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
- }
- NSUInteger row=[indexPath row];
- //添加图标
- cell.imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[iconItems objectAtIndex:row]]];
- cell.textLabel.text=[dataItems objectAtIndex:row];
- //添加明细
- cell.detailTextLabel.text =[detailItems objectAtIndex:row];
- //导航按钮
- cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton;
- return cell;
- }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { //实例化cell时注意样式由原来的UITableViewCellStyleDefault缺省改成UITableViewCellStyleSubtitle cell = [[[UITableViewCell alloc] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSUInteger row=[indexPath row]; //添加图标 cell.imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@.gif",[iconItems objectAtIndex:row]]]; cell.textLabel.text=[dataItems objectAtIndex:row]; //添加明细 cell.detailTextLabel.text =[detailItems objectAtIndex:row]; //导航按钮 cell.accessoryType= UITableViewCellAccessoryDetailDisclosureButton; return cell; }
二、现在导航按钮已经有了但是只是个空按钮,下一步我们添加其功能
1、添加NextControlView类文件及NextControlView.xib文件,还有准备所需图片
2、编辑NextControlView.m
- - (void)viewDidLoad {
- self.imgArray = [[NSMutableArray alloc] init];
- for(int i=1;i<=3;i++)
- {
- NSString *imgName =[NSString stringWithFormat:@"img%d.jpg",i];
- [self.imgArray addObject:imgName];
- }
- [self.imgArray release];
- UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 436)];
- [img setImage:[UIImage imageNamed:[self.imgArray objectAtIndex:0]]];
- self.imgView = img;
- [self.imgView setContentMode:UIViewContentModeScaleAspectFit];
- [self.view addSubview:imgView];
- [img release];
- [imgView setImage:[UIImage imageNamed:[self.imgArray objectAtIndex:Page]]];
- self.title = [NSString stringWithFormat:@"image %@",[self.imgArray objectAtIndex:Page]];
- [super viewDidLoad];
- }
- (void)viewDidLoad { self.imgArray = [[NSMutableArray alloc] init]; for(int i=1;i<=3;i++) { NSString *imgName =[NSString stringWithFormat:@"img%d.jpg",i]; [self.imgArray addObject:imgName]; } [self.imgArray release]; UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 436)]; [img setImage:[UIImage imageNamed:[self.imgArray objectAtIndex:0]]]; self.imgView = img; [self.imgView setContentMode:UIViewContentModeScaleAspectFit]; [self.view addSubview:imgView]; [img release]; [imgView setImage:[UIImage imageNamed:[self.imgArray objectAtIndex:Page]]]; self.title = [NSString stringWithFormat:@"image %@",[self.imgArray objectAtIndex:Page]]; [super viewDidLoad]; }
3、在TableViewDemo1ViewController.m中增加如下导航按钮的处理代码,
- -(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
- NSInteger row = indexPath.row;
- nextControlView = [[NextControlView alloc] initWithNibName:@"NextControlView" bundle:nil];
- nextControlView.Page=row;
- [self.navigationController pushViewController:nextControlView animated:YES];
- }
-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{ NSInteger row = indexPath.row; nextControlView = [[NextControlView alloc] initWithNibName:@"NextControlView" bundle:nil]; nextControlView.Page=row; [self.navigationController pushViewController:nextControlView animated:YES]; }
好,现在我们运行程序试一试
但是经过试验点击导航按钮时没有任何反应,这时怎么回事呢?我们再仔细分析一下上面的导航按钮处理函数,注意最后一行代码:[self.navigationController pushViewController:nextControlView animated:YES];
是做切换画面功能的
那么self.navigationController 是什么?其实至此我们落掉了一个很重要的控制器即导航控制器,因为self 必须在navigation栈中,self.navigationController才不会为空,才可以帮助我们转化画面。下面我们来加上这个navigationController
首先更改TableViewDemo1AppDelegate.h代码如下:
增加navigationController 去掉viewController
- @interface TableViewDemo1AppDelegate : NSObject <UIApplicationDelegate> {
- UIWindow *window;
- //TableViewDemo1ViewController *viewController;
- UINavigationController *navigationController;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- //@property (nonatomic, retain) IBOutlet TableViewDemo1ViewController *viewController;
- @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
- @end
@interface TableViewDemo1AppDelegate : NSObject <UIApplicationDelegate> { UIWindow *window; //TableViewDemo1ViewController *viewController; UINavigationController *navigationController; } @property (nonatomic, retain) IBOutlet UIWindow *window; //@property (nonatomic, retain) IBOutlet TableViewDemo1ViewController *viewController; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @end
在TableViewDemo1AppDelegate.m修改didFinishLaunchingWithOption函数如下
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- //[self.window addSubview:viewController.view];
- [window addSubview:[navigationController view]];
- [self.window makeKeyAndVisible];
- return YES;
- }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //[self.window addSubview:viewController.view]; [window addSubview:[navigationController view]]; [self.window makeKeyAndVisible]; return YES; }
打开MainWindow.xib文件在IB设计器中删除原来的Table View Demo1 View Controller
增加一个Navigation Controller并且设置View Controller (Root View Controller)关联到TableViewDemo1ViewController
最后选择TableViewDemo1 App Delegate
如下图左键选中navigationController到Table View Demo1 View Controller做关联
关联后
OK至此我们导航控制器配置完毕,运行程序
分别点击中国、美国、日本一切正常显示见下图: