29.UITableView表视图第四章(添加高级)

准备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];

}

设置每区多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return self.dataArray.count;

}

- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *identifier = @”Mycell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier] autorelease];

}

展示数据

CellModel *model = self.dataArray[indexPath.row];

cell.textLabel.text = model.foodName;

cell.detailTextLabel.text = model.money;

return cell;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值