新建程序 到story board 中 拖个Navigation 进来 默认Navigation 的root controller 是table view controller 我们可以改成别的 上章就是改成最普通的 view controller 这次我们就以table view controller 做根界面 所以可以不用改
然后再拖个 uiviewcontroller 进来 做具体显示界面
点击 table view 中的 cell 拉线到other controller 选择 push
然后我们直接设置 cell 中的一些属性
identifier 记得要填上 要不然不会 cell 不会实现 push segue
新建 一个 tablecontoller 继承 UITableViewController
UITableViewController 默认把Controller中的 TableView 的 dataSource 和delegate 事件 绑定到自身中
如果 是你自己拖的 UITableView 可以在界面上 直接拖绑定 也可以用代码写
tableView.delegate =self;
tableView.dataSource =self;
self 可以是任意对象 一般为了根好了解这类可以做什么 我们可以这样写 来表达 这类所实现的借口#import<UIKit/UIKit.h>
@interface testViewController :UITableViewController<UITableViewDelegate,UITableViewDataSource>
@end
下面是代码
//
// testViewController.m
// test
//
// Created by mac on 12-4-23.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import"testViewController.h"
#import"otherController.h"
@interfacetestViewController ()
{
NSArray* array;
}
@end
@implementation testViewController
- (void)viewDidLoad
{
array = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[superviewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}
//返回组的数量
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
//返回section组的子项数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return array.count;
}
//返回当前分组的组名
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"第%d组",section];
}
//返回具体的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* CellIdentifier =@"Cell"; //可以随便填 tableview 通过这个identifier 来复用你的控件 如果你有跳转的一些事件 要跟界面上的一致
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
//初始化tableviewcell 主要是 style 显示的样式
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
//设置 内容
cell.textLabel.text = [array objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
//通过 segue跳转的 回调方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"TableCellToOther"])
{
NSIndexPath* path = [self.tableView indexPathForSelectedRow];
NSInteger row = path.row;
NSString* title = [NSString stringWithFormat:@"第%d组,%@",path.section,[array objectAtIndex:row]];
//可以通过 segue.destinationviewcontroller获得跳转的下一个controller 可以直接调用其方法
//记得 加上头文件 #import "otherController.h"
[segue.destinationViewController showTitle:title];
}
}
@end
-(void)showTitle:(NSString *)title
{
self.title = title;
}
运行界面
其实 你在建立项目时选择 master-detail application 你会发现里面的效果是差不多的。 像翻页的效果可以建个 page based application 自己看下里面的实现 苹果很多动画都是封装好的 调用很方便 至少我现在觉得比 android 动画效果好看也容易实现