准备1.创建一个NSObject的普通类CellModel
在CellModel.h中
@interface CellModel : NSObject
@property (nonatomic,retain)NSString *foodName;
@property (nonatomic,retain)NSString *money;
@end
在CellModel.m中
@implementation CellModel
- (void)dealloc
{
[_foodName release];
[_money release];
[super dealloc];
}
保护方法
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
@end
准备2.创建一个继承UIViewController的类SecondViewController
在SecondViewController.h中
// 创建一个协议
@protocol SecondViewDelegate < NSObject>
- (void)addCellModel:(CellModel *)model;
@end
@interface SecondViewController : UIViewController
@property (nonatomic,assign)id < SecondViewDelegate >delegate;
@end
在SecondViewController.m中
遵守UITextFieldDelegate协议
@interface SecondViewController ()< UITextFieldDelegate>
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
textField.backgroundColor = [UIColor yellowColor];
textField.delegate = self;
[self.view addSubview:textField];
[textField release];
}
实现点击Return键触发传值
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
CellModel *model = [[CellModel alloc] init];
model.foodName = textField.text;
model.money = @”123”;
把model传过去
if ([_delegate respondsToSelector:@selector(addObject:)]) {
[_delegate addCellModel:model];
};
[self.navigationController popViewControllerAnimated:YES];
return YES;
}
准备完了
在根视图控制器中 在RootTableViewController.m文件中
# import “RootTableViewController.h”
# import “CellModel.h”
# import “SecondViewController.h”
遵守SecondViewDelegate协议
@interface RootTableViewController ()< SecondViewDelegate>
保存model的数组
@property (nonatomic,retain)NSMutableArray *dataArray;
@property (nonatomic,retain)SecondViewController *secondVC;
@end
@implementation RootTableViewController
- (void)dealloc
{
[_secondVC release];
[_dataArray release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
TableViewController 自带tableView 作为self.view
并且 实现部分代理方法 和 数据源方法(代理 和 数据源代理已经帮你遵守了)
[self setUpData];
[self addBarButton];
}
初始化数据
- (void)setUpData
{
NSArray *arrA = @[@{@”foodName”:@”鱼香肉丝”,@”money”:@”15 元”},
@{@”foodName”:@”宫保鸡丁”,@”money”:@”18 元”},
@{@”foodName”:@”小炒肉片”,@”money”:@”13 元”},
@{@”foodName”:@”水煮肉”,@”money”:@”12 元”},
@{@”foodName”:@”鸡蛋西红柿”,@”money”:@”23 元”},
@{@”foodName”:@”青椒肉丝”,@”money”:@”34 元”}];
self.dataArray = [NSMutableArray array];
循环数组
for (NSDictionary *oneDic in arrA) {
KVC间接赋值
CellModel *model = [[CellModel alloc] init];
[model setValuesForKeysWithDictionary:oneDic];
装model
[self.dataArray addObject:model];
[model release];
}
}
- (void)addBarButton
{
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@”添加” style:(UIBarButtonItemStylePlain) target:self action:@selector(leftButton:)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@”编辑” style:(UIBarButtonItemStylePlain) target:self action:@selector(rightButton:)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
[rightButton release];
}
- (void)leftButton:(UIBarButtonItem *)leftButton
{
self.secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:self.secondVC animated:YES];
self.secondVC.delegate = self;
[self.secondVC release];
}
- (void)addCellModel:(CellModel *)model
{
[self.dataArray addObject:model];
[self.tableView reloadData];
}
- (void)rightButton:(UIBarButtonItem *)rightButton
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
判断编辑状态
if (self.tableView.editing == YES) {
rightButton.title = @”完成”;
}else{
rightButton.title = @”编辑”;
}
}
允许编辑
- (BOOL)tableView:(UITableView )tableView canEditRowAtIndexPath:(NSIndexPath )indexPath {
return YES;
}
设置编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView )tableView editingStyleForRowAtIndexPath:(NSIndexPath )indexPath
{
return UITableViewCellEditingStyleDelete;
}
完成编辑
- (void)tableView:(UITableView )tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath )indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.dataArray removeObjectAtIndex:indexPath.row];
}
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}