iOS设计之 表格UITableView 的参数与视图页面之间的相互传递
设计思路:创建一个表格 ,在表格中用集合创建数据源 ,点击表格中的参数,并将之传递给一个视图页面,在视图页面中创建一个文本框用于接收传递的结果,之后对就到的参数进行修改,并将之传回表格中,同上步骤可以继续操作;
代码实现如下;
1、 AppDelegate.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *VC=[[ViewController alloc]init];
//设置导航栏
UINavigationController *navigatonC=[[UINavigationController alloc]initWithRootViewController:VC];
self.window.rootViewController=navigatonC;
return YES;
return YES;
}
2、创建表格的视图页面
ViewController.h
#import <UIKit/UIKit.h>
#import "FirstViewController.h"
//遵循协议
#import "FirstViewController.h"
//遵循协议
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,posValueDlegate>
@property(strong,nonatomic)UITableView *tableV;//表格
@property(strong,nonatomic)NSMutableArray *Array;//存数据源的集合
@property(strong,nonatomic)NSString *string;
@property(assign,nonatomic)int num;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor=[UIColor greenColor];
self.title=@"通信录";
//数据源
self.Array=[NSMutableArray arrayWithCapacity:20];
[self.Array addObject:@"AAAAAA"];
[self.Array addObject:@"BBBBBB"];
[self.Array addObject:@"CCCC"];
[self.Array addObject:@"DDDDDD"];
[self.Array addObject:@"EEEEEE"];
//初始化表格
self.tableV=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableV.backgroundColor=[UIColor whiteColor];
//代理
self.tableV.delegate=self;
self.tableV.dataSource=self;
//添加
[self.view addSubview:self.tableV];
//重用单元标识
[self.tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.tableV.separatorColor=[UIColor blueColor];
//按钮项
UIBarButtonItem *barButtontiem=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
self.navigationItem.rightBarButtonItem=barButtontiem;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.Array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
cell.textLabel.text=self.Array[indexPath.row];
return cell;
}
//选中行的内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",self.Array[indexPath.row]);
//把集合里的元素提取出来赋值给 string
self.string=self.Array[indexPath.row];
}
-(void)nextPage
{
FirstViewController *firstVC=[[FirstViewController alloc]init];
firstVC.str=self.string;
//代理
firstVC.delegate=self;
//切换到下一页面
[self.navigationController pushViewController:firstVC animated:YES];
}
//实现协议方法
-(void)posValueDlegate:(NSString *)str
{
//把firstViewController传过来的值添加到集合 array 中
[self.Array replaceObjectAtIndex:self.num withObject:str];
[self.tableV reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor=[UIColor greenColor];
self.title=@"通信录";
//数据源
self.Array=[NSMutableArray arrayWithCapacity:20];
[self.Array addObject:@"AAAAAA"];
[self.Array addObject:@"BBBBBB"];
[self.Array addObject:@"CCCC"];
[self.Array addObject:@"DDDDDD"];
[self.Array addObject:@"EEEEEE"];
//初始化表格
self.tableV=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
self.tableV.backgroundColor=[UIColor whiteColor];
//代理
self.tableV.delegate=self;
self.tableV.dataSource=self;
//添加
[self.view addSubview:self.tableV];
//重用单元标识
[self.tableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
self.tableV.separatorColor=[UIColor blueColor];
//按钮项
UIBarButtonItem *barButtontiem=[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(nextPage)];
self.navigationItem.rightBarButtonItem=barButtontiem;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.Array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=@"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellidentifier forIndexPath:indexPath];
cell.textLabel.text=self.Array[indexPath.row];
return cell;
}
//选中行的内容
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",self.Array[indexPath.row]);
//把集合里的元素提取出来赋值给 string
self.string=self.Array[indexPath.row];
}
-(void)nextPage
{
FirstViewController *firstVC=[[FirstViewController alloc]init];
firstVC.str=self.string;
//代理
firstVC.delegate=self;
//切换到下一页面
[self.navigationController pushViewController:firstVC animated:YES];
}
//实现协议方法
-(void)posValueDlegate:(NSString *)str
{
//把firstViewController传过来的值添加到集合 array 中
[self.Array replaceObjectAtIndex:self.num withObject:str];
[self.tableV reloadData];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be
}
@end
3、接收表格参数的视图
FirstViewController.h
#import <UIKit/UIKit.h>
//声明协议
@protocol posValueDlegate<NSObject>
-(void)posValueDlegate:(NSString *)str;
-(void)posValueDlegate:(NSString *)str;
@end
@interface FirstViewController : UIViewController<UITextFieldDelegate>
@property(strong,nonatomic)NSString *str;
@property(strong,nonatomic)id<posValueDlegate>delegate;
@property(strong,nonatomic)UITextField *textName;
@property(strong,nonatomic)NSString *str;
@property(strong,nonatomic)id<posValueDlegate>delegate;
@property(strong,nonatomic)UITextField *textName;
@end
FirstViewController.m
#import "FirstViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
self.title=@"通信录";
//按钮项
UIBarButtonItem *barButtonTtem=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(BackPage)];
self.navigationItem.leftBarButtonItem=barButtonTtem;
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
self.textName.borderStyle=1;
//代理
self.textName.delegate=self;
self.textName.text=self.str;
[self.view addSubview:self.textName];
}
-(void)BackPage
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor yellowColor];
self.title=@"通信录";
//按钮项
UIBarButtonItem *barButtonTtem=[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(BackPage)];
self.navigationItem.leftBarButtonItem=barButtonTtem;
//创建文本框
self.textName=[[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
self.textName.borderStyle=1;
//代理
self.textName.delegate=self;
self.textName.text=self.str;
[self.view addSubview:self.textName];
}
-(void)BackPage
{
//切换页面
[self.navigationController popToRootViewControllerAnimated:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//调用代理
if (self.delegate) {
[self.delegate posValueDlegate:self.textName.text];
}
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//调用代理
if (self.delegate) {
[self.delegate posValueDlegate:self.textName.text];
}
//隐藏键盘
if ([textField isFirstResponder]) {
[textField resignFirstResponder];
}
return YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
4、效果
点中表格中的内容——》接收到内容
修改之后的内容—————》表格中接收到的内容