上一篇我们学习了导航控制器和表视图的框架建立,并实现了简单的分栏和视图控制器之间的push.下面就在上一篇的基础上整体的介绍如何设计一个具备完善知识点的app.
目标:1.展示按钮视图:(上一篇介绍过)
2.校验表视图
3.行控制视图
4.可移动行视图
5.可删除行视图
6.可编辑详细信息的视图
下面就2-6一一来讲解.
2.校验表视图
这个视图的控制器是
BIDCheckListController
因为这个视图是一个checkout视图.(也就是打一个 对号.)所以我们还是继承 BIDSecondLevelViewController
看看我们的.h文件:
#import "BIDSecondViewController.h"
@interface BIDCheckListController : BIDSecondViewController
@property (nonatomic,strong)NSArray *list;
@property(nonatomic,strong)NSIndexPath *lastIndexPath;
@end
也许你还不明白第二个属性的意思:lastIndexpath,先透露一下,这个属性其实就是记录你选择的行的indexpath.当我们选择其他的行的时候,lastIndexpath 所指向的行的 check 就会消失.(函数实现)
接着就开始了我们的.m文件的实现
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"Who Hash",
@"Bubba Gump Shrimp Étouffée", @"Who Pudding", @"Scooby Snacks",
@"Everlasting Gobstopper", @"Green Eggs and Ham", @"Soylent Green",
@"Hard Tack", @"Lembas Bread", @"Roast Beast", @"Blancmange", nil];
self.list = array;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CheckMarkCellIdentifiter = @"CheckMarkCellIdentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CheckMarkCellIdentifiter];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [list objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
return cell;
}
数据的加载,行的设置(列是plain类型的,就不再设置啦),和cell的建立.都是我们熟悉的内容了.细心地你有没有发现这里面多了一部分内容呢?
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];//记录我们上一次check的indenpath.并还原row的值
cell.textLabel.text = [list objectAtIndex:row];
cell.accessoryType = (row == oldRow && lastIndexPath != nil)?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
//判断当前的row和原来的row是否相等,如果相等,则checkmark.
return cell;
//下面就是对轩中行的处理.
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
int newRow = [indexPath row];
int oldRow = (lastIndexPath == nil )? [lastIndexPath row]:-1;
if (newRow != oldRow) {
UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath];
newCell.accessoryType = UITableViewCellAccessoryCheckmark;
UITableViewCell *oldCell = [tableView cellForRowAtIndexPath:lastIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
lastIndexPath = indexPath;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
重:每个视图建立之后都要加载到我们的root视图 firstLevelViewController里面.代码如下:这可是本app的全部加载代码哦.找找看,一定有你想要的.(以下的视图不再陈述该代码)
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Frist Level";
NSMutableArray *array = [[NSMutableArray alloc]init];
//BIDDisclosureButtonController
BIDDisclosureButtonController *disclosureButtonController = [[BIDDisclosureButtonController alloc]initWithStyle:UITableViewStylePlain];
disclosureButtonController.title = @"disclosure Button";
disclosureButtonController.rowImage = [UIImage imageNamed:@"disclosureButtonControllerIcon.png"];
[array addObject:disclosureButtonController];
//BIDCheckListController
BIDCheckListController *checkListController = [[BIDCheckListController alloc]initWithStyle:UITableViewStylePlain];
checkListController.title = @"check List";
checkListController.rowImage = [UIImage imageNamed:@"checkmarkControllerIcon.png"];
[array addObject:checkListController];
//BIDRowControlsController
//
BIDRowControlsController *rowControlsController = [[BIDRowControlsController alloc]initWithStyle:UITableViewStylePlain];
rowControlsController.title = @"row Tap";
rowControlsController.rowImage = [UIImage imageNamed:@"rowControlsIcon.png"];
[array addObject:rowControlsController];
//BIDMoveMeController
BIDMoveMeController *moveMeController = [[BIDMoveMeController alloc]initWithStyle:UITableViewStylePlain];
moveMeController.title = @"Move me";
moveMeController.rowImage = [UIImage imageNamed:@"moveMeIcon.png"];
[array addObject:moveMeController];
//BIDDeleteMeController
BIDDeleteMeController *deleteMeController = [[BIDDeleteMeController alloc]initWithStyle:UITableViewStylePlain];
deleteMeController.title =@"delete Me";
deleteMeController.rowImage = [UIImage imageNamed:@"deleteMeIcon.png"];
[array addObject:deleteMeController];
BIDPresidentsViewController *presidentViewController = [[BIDPresidentsViewController alloc]initWithStyle:UITableViewStylePlain];
presidentViewController.title = @"Detail Edit";
presidentViewController.rowImage = [UIImage imageNamed:@"detailEditIcon.png"];
[array addObject:presidentViewController];
self.controllers = array;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
3.行控制视图
行控制视图简单的说:就是在cell上面加一个button.我们点击cell 和button的两种不同的显示
#import "BIDSecondViewController.h"
@interface BIDRowControlsController : BIDSecondViewController
@property(nonatomic,strong)NSArray *list;
-(IBAction)buttonTapped:(id)sender ;
@end
没有xib文件我们是如何使用这个 buttonTapped 接口 呢? 实话说,这不是和空间拖拽连接的,而是通过 addtarget代码来链接的..m中会实现的,别着急哦
-(IBAction)buttonTapped:(id)sender
{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButton superview];
NSUInteger buttonRow = [[self.tableView indexPathForCell:buttonCell] row];
NSString *buttonTitle = [list objectAtIndex:buttonRow];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"you has tap the button" message:[NSString stringWithFormat:@"you has tap the %@ button",buttonTitle] delegate:nil cancelButtonTitle:@" i kown" otherButtonTitles:nil];
[alert show];
}
下面是常规的函数实现:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"R2-D2",
@"C3PO", @"Tik-Tok", @"Robby", @"Rosie", @"Uniblab",
@"Bender", @"Marvin", @"Lt. Commander Data",
@"Evil Brother Lore", @"Optimus Prime", @"Tobor", @"HAL",
@"Orgasmatron", nil];
self.list = array;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
好好看看以下的cell协议实现的代码,你就明白buttontagged是如何联系上的.
我们首先在cell的建立中建立了一个button,然后通过
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
来实现 button 和button tagged的联系.有没有明白呢?
具体的实现代码如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *rowTapIdentifiter = @"RowTapIdentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rowTapIdentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rowTapIdentifiter];
UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
UIImage *buttonupImage = [UIImage imageNamed:@"button_up.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, buttonupImage.size.width, buttonupImage.size.height);
[button setBackgroundImage:buttonupImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
[button setTitle:@"tap" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
}
NSInteger row = [indexPath row];
NSString *rowTitle = [list objectAtIndex:row];
cell.textLabel.text = rowTitle;
return cell;
}
下一个问题:当我们点击了cell 而没有点击tap,会发生什么呢 有好的提示如下:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *rowTitle = [list objectAtIndex:row];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"tap Pressed" message:[NSString stringWithFormat:@"you has pressed the %@",rowTitle] delegate:nil cancelButtonTitle:@"yes i know" otherButtonTitles:nil];
[alert show];
[tableView deselectRowAtIndexPath:indexPath animated:YES];//???
// 有没有遇到过,导航+UITableView,在push,back回来之后,当前cell仍然是选中的状态。
// 当然,解决办法简单,添加一句[tableView deselectRowAtIndexPath:indexPath animated:YES]即可。
// 令人纠结的时,在没加这句的时候,有的视图同样回来之后,选中状态消失,为什么会出现这种情况呢?
// 原来是,如果UITableView是在UITableViewController中时,就会默然取消,而如果是在UIViewController时,需要添加这一句,不过有时即使前者也需要添加,那是因为在视图加载时有其它功能代码,具体情况各异。所以后者必须加,前者可能需要加。
//
// 当然如果要求高的话,另外一种更加理想的办法是:
// - (void) viewWillAppear: (BOOL)inAnimated {
// NSIndexPath *selected = [self.table indexpathForSelectedRow];
// if(selected) [self.table deselectRowAtIndexpath:selected animated:NO];
// }
//
// 这种方法是在放回的过程中逐渐取消选中状态的,可以提示刚才点进去的是哪一行,默认的也正是这种效果。
//
}
这就是全部代码了,贴到xcode上,试试看吧~~
4.可移动行视图 -- 我们只是多实现了几个协议的函数而已
老样子,.h文件
#import "BIDSecondViewController.h"
@interface BIDMoveMeController : BIDSecondViewController
@property(nonatomic,strong)NSMutableArray *list;
-(IBAction)taggleMove:(id)sender ;
@end
我们看代码就明白了:
当tableview处于编辑状态的时候,我们的导航栏的右边会出现一个 Done 键,以便于Move完成后 储存.
当处于非编辑状态时候,右边是一个Move ,提示我们可以点击move进行编辑.
-(IBAction)taggleMove:(id)sender
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
if (self.tableView.editing) {
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
}
else
[self.navigationItem.rightBarButtonItem setTitle:@"Move"];
}
下面是文件的加载:我们在里面对button -Move 的实现做了处理.
- (void)viewDidLoad
{
[super viewDidLoad];
if (list == nil) {
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"R2-D2",
@"C3PO", @"Tik-Tok", @"Robby", @"Rosie", @"Uniblab",
@"Bender", @"Marvin", @"Lt. Commander Data",
@"Evil Brother Lore", @"Optimus Prime", @"Tobor", @"HAL",
@"Orgasmatron", nil];
self.list = array;
}
// Do any additional setup after loading the view.
UIBarButtonItem *moveButton = [[UIBarButtonItem alloc]
initWithTitle:@"Move"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(taggleMove:)];
self.navigationItem.rightBarButtonItem = moveButton;
}
以下就是cell的建立和 row的number的处理啦.
先看看代码:
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MoveCellIdentifiter = @"MoveCellIdentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MoveCellIdentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MoveCellIdentifiter];
cell.showsReorderControl = YES;//?要重新排序组件,必须在单元上设置一个属性
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [list objectAtIndex:row];
return cell;
}
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleNone;
//表示我们不希望更改或者删除任何行
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//这个就是 move的操作了,是不是很简单呢 ?
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSInteger fromRow = [sourceIndexPath row];
NSInteger toRow = [destinationIndexPath row];
id object = [list objectAtIndex:fromRow];
[list removeObjectAtIndex:fromRow];
[list insertObject:object atIndex:toRow];
}
5.可删除行视图
可删除行视图和move很相似哦
#import "BIDSecondViewController.h"
@interface BIDDeleteMeController : BIDSecondViewController
@property(nonatomic,strong)NSMutableArray *list;
-(IBAction)taggleDelete:(id)sender ;
@end
先看看一些一样的代码:我在里面做了注释:
-(IBAction)taggleDelete:(id)sender
{
[self.tableView setEditing:!self.tableView.editing animated:YES];
if (self.tableView.editing) {
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
}
else
[self.navigationItem.rightBarButtonItem setTitle:@"Delete"];
}
//这个和move很相似.
//我们在加载文件的时候没有使用了固定数据,而是直接使用了 plist文件读取.
- (void)viewDidLoad
{
[super viewDidLoad];
if (list == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"computers" ofType:@"plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.list = array;
}
UIBarButtonItem *editButton = [[UIBarButtonItem alloc]initWithTitle:@"Delete" style:UIBarButtonItemStyleBordered target:self action:@selector(taggleDelete:)];
//设置了 删除的图标等
self.navigationItem.rightBarButtonItem = editButton;
// Do any additional setup after loading the view.
}
下面就是cell的实现啦:注释解释
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *DeleteCellIdentifiter = @"DeleteCellIdentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteCellIdentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DeleteCellIdentifiter];
}
NSInteger row = [indexPath row];
cell.textLabel.text= [list objectAtIndex:row];
return cell;
}
//cell的实现没什么可说的,很中规中矩的代码.
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.list removeObjectAtIndex:[indexPath row]];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
上面的这个函数实现就是delete的实现.首先把我们选中的row在 list中remove掉,然后在tableview中的显示也删除.
6.可编辑详细信息的视图
l这个视图相对来说比较难:
首先我们先建立一个 president.h.m的模型,用于 存储数据到硬盘和读出
#import <Foundation/Foundation.h>
#define kPresidentNumberKey @"President"
#define kPresidentNameKey @"Name"
#define kPresidentFromKey @"FromYear"
#define kPresidentToKey @"ToYear"
#define kPresidentPartyKey @"Party"
@interface BIDPresident : NSObject<NSCoding>
@property int number;
@property (nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *fromYear;
@property(nonatomic,copy)NSString *toYear;
@property(nonatomic,copy)NSString *party;
@end
.m文件
#import "BIDPresident.h"
@implementation BIDPresident
@synthesize number,name,fromYear,toYear,party;
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:self.number forKey:kPresidentNumberKey];
[aCoder encodeObject:self.name forKey:kPresidentNameKey ];
[aCoder encodeObject:self.fromYear forKey:kPresidentFromKey];
[aCoder encodeObject:self.toYear forKey:kPresidentToKey];
[aCoder encodeObject:self.party forKey:kPresidentPartyKey];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
number = [aDecoder decodeIntForKey:kPresidentNumberKey];
name = [aDecoder decodeObjectForKey:kPresidentNameKey];
fromYear = [aDecoder decodeObjectForKey:kPresidentFromKey];
toYear = [aDecoder decodeObjectForKey:kPresidentToKey];
party = [aDecoder decodeObjectForKey:kPresidentPartyKey];
}
这无非就是个编解码的工作.
#import "BIDSecondViewController.h"
@interface BIDPresidentsViewController : BIDSecondViewController
@property (strong,nonatomic)NSMutableArray *list;
@end
我们的.h文件主要是存储了一个可变的数组,然后点击行之后跳转到另一个viewCOntroller中实现修改.
#import "BIDPresidentsViewController.h"
#import "BIDPresidentDetailController.h"
#import "BIDPresident.h"
@interface BIDPresidentsViewController ()
@end
@implementation BIDPresidentsViewController
@synthesize list;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle]pathForResource:@"Presidents" ofType:@"plist"];
NSData *data;
NSKeyedUnarchiver *unarchiver;
data = [[NSData alloc]initWithContentsOfFile:path];
unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data];
NSMutableArray *array = [unarchiver decodeObjectForKey:@"Presidents"];
self.list = array;
[unarchiver finishDecoding];
// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [list count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *PresidentListCellIdentifiter = @"PresidentListCellIdentifiter";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PresidentListCellIdentifiter];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PresidentListCellIdentifiter];
}
NSUInteger row = [indexPath row];
BIDPresident *thePres = [self.list objectAtIndex:row];
cell.textLabel.text = thePres.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ -- %@",thePres.fromYear,thePres.toYear];
//上一行实现了 detail .也就是在一个主名下面简介显示一些内容
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
BIDPresident *prez = [self.list objectAtIndex:row];
BIDPresidentDetailController *childController = [[BIDPresidentDetailController alloc]initWithStyle:UITableViewStylePlain];
childController.title = prez.name;
childController.president = prez;
[self.navigationController pushViewController:childController animated:YES];
}
//这个函数就是在我们点击了indexPath之后,我们需要显示子视图了.
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
下面是detail.h文件
#import <UIKit/UIKit.h>
@class BIDPresident;
#define kNumberOfEditableRows 4
#define kNameRowIndex 0
#define kFromYearRowIndex 1
#define kToyearRowIndex 2
#define kpartyIndex 3
#define KLabelTag 4096
@interface BIDPresidentDetailController : UITableViewController<UITextFieldDelegate>
//
@property (strong,nonatomic)BIDPresident *president;
@property(strong,nonatomic)NSArray *fieldLabels;
@property(strong,nonatomic)NSMutableDictionary *tempValues;
@property(strong,nonatomic)UITextField *currentTextField;
-(IBAction)cancel:(id)sender ;
-(IBAction)save:(id)sender ;
-(IBAction)textFieldDone :(id)sender;
@end
我们定义了president相应的属性,以便于我们进行修改:
#import "BIDPresidentDetailController.h"
#import "BIDPresident.h"
@interface BIDPresidentDetailController ()
@end
@implementation BIDPresidentDetailController
@synthesize president,fieldLabels,tempValues,currentTextField;
-(IBAction)cancel:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
//将最顶层的controller出栈.也就是回到上一个controller.
}
-(IBAction)save:(id)sender
{ 当我们编辑textfield的时候,就会将正在编辑的textfield 给currenttextfield.
if (currentTextField != nil) {
NSNumber *tagNum = [NSNumber numberWithInt:currentTextField.tag];
[tempValues setObject:currentTextField.text forKey:tagNum];
}
//for循环改变所有cell的值
for (NSNumber *key in [tempValues allKeys]) {
switch ([key intValue]) {
case kNameRowIndex:
president.name = [tempValues objectForKey:key];
break;
case kFromYearRowIndex:
president.fromYear = [tempValues objectForKey:key];
break;
case kToyearRowIndex:
president.toYear = [tempValues objectForKey:key];
case kpartyIndex:
president.party= [tempValues objectForKey:key];
break;
default:
break;
}
}
[self.navigationController popViewControllerAnimated:YES];
NSArray *allControllers = self.navigationController.viewControllers;
UITableViewController *parent = [allControllers lastObject];
[parent.tableView reloadData];
}
-(IBAction)textFieldDone :(id)sender
{
[sender resignFirstResponder];
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc]initWithObjects:@"Name:",@"From:",@"To:",@"Party:", nil];
self.fieldLabels = array;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]initWithTitle:@"Save" style:UIBarButtonItemStyleDone target:self action:@selector(save:)];
self.navigationItem.rightBarButtonItem = saveButton;
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
self.tempValues = dict;
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return kNumberOfEditableRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 75, 75)];
// label.textAlignment = UITextAlignmentRight;
label.tag = KLabelTag;
label.font = [UIFont boldSystemFontOfSize:15];
[cell.contentView addSubview:label];
//----
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(90, 12, 200, 25)];
textField.clearsOnBeginEditing = NO;
[textField setDelegate:self];
textField.returnKeyType = UIReturnKeyDone;
[textField addTarget:self action:@selector(textFieldDone:) forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentView addSubview:textField];
}
NSUInteger row = [indexPath row];
UILabel *label = (UILabel *)[cell viewWithTag:KLabelTag];
UITextField *textfield = nil;
for (UIView *oneView in cell.contentView.subviews ) {
if ([oneView isMemberOfClass:[UITextField class]])
textfield = (UITextField *)oneView;
}
label.text = [fieldLabels objectAtIndex:row];
NSLog(@"label :%@",label.text);
NSNumber *rowAsNum = [NSNumber numberWithInt:row];
NSLog(@"row :%lu",(unsigned long)row);
switch (row) {
case kNameRowIndex:
if ([[tempValues allKeys] containsObject:rowAsNum])
textfield.text = [tempValues objectForKey:rowAsNum];
else
textfield.text = president.name;
break;
case kFromYearRowIndex:
if ([[tempValues allKeys] containsObject:rowAsNum])
textfield.text = [tempValues objectForKey:rowAsNum];
else
textfield.text = president.fromYear;
break;
case kToyearRowIndex:
if ([[tempValues allKeys] containsObject:rowAsNum])
textfield.text = [tempValues objectForKey:rowAsNum];
else
textfield.text = president.toYear;
break;
case kpartyIndex:
if ([[tempValues allKeys] containsObject:rowAsNum])
textfield.text = [tempValues objectForKey:rowAsNum];
else
textfield.text = president.party;
default:
break;
}
if (currentTextField == textfield) {
currentTextField = nil;
}
textfield.tag = row;
// Configure the cell...
return cell;
}
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
return nil;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
self.currentTextField = textField;
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSNumber *tagAsNum = [NSNumber numberWithInt:textField.tag];
[tempValues setObject:textField.text forKey:tagAsNum];
}