//
// test15AppDelegate.h
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
@interface test15AppDelegate :NSObject <UIApplicationDelegate> {
UINavigationController *navController;
}
@property (nonatomic,retain) IBOutletUIWindow *window;
@property (nonatomic,retain) IBOutletUINavigationController *navController;
@end
//
// test15AppDelegate.m
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"test15AppDelegate.h"
@implementation test15AppDelegate
@synthesize window=_window;
@synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.windowaddSubview:navController.view];
[self.windowmakeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
*/
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
/*
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
}
- (void)applicationWillTerminate:(UIApplication *)application
{
/*
Called when the application is about to terminate.
Save data if appropriate.
See also applicationDidEnterBackground:.
*/
}
- (void)dealloc
{
[navControllerrelease];
[_windowrelease];
[superdealloc];
}
@end
//
// FirstLevelViewController.h
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
@interface FirstLevelV_iewController :UITableViewController {
NSArray *controllers;
}
@property (nonatomic,retain) NSArray *controllers;
@end
//
// FirstLevelViewController.m
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"FirstLevelViewController.h"
#import"SecondLevelViewController.h"
#import"DisclosureButtonController.h"
#import"CheckListController.h"
#import"RowControlsController.h"
#import"MoveMeController.h"
#import"DeleteMeController.h"
#import"PresidentViewController.h"
@implementation FirstLevelV_iewController
@synthesize controllers;
- (void)viewDidLoad{
self.title =@"First Level";
NSMutableArray *array = [[NSMutableArrayalloc] init];
//Disclosue Button
DisclosureButtonController *disclosureButtonController = [[DisclosureButtonControlleralloc] initWithStyle:UITableViewStylePlain];
disclosureButtonController.title =@"Disclosure Buttons";
disclosureButtonController.rowImage = [UIImageimageNamed:@"disclosureButtonControllerIcon.png"];
[array addObject:disclosureButtonController];
[disclosureButtonControllerrelease];
//Checklist
CheckListController *checkListController = [[CheckListControlleralloc] initWithStyle:UITableViewStylePlain];
checkListController.title =@"Check One";
checkListController.rowImage = [UIImageimageNamed:@"checkmarkControllerIcon.png"];
[array addObject:checkListController];
[checkListControllerrelease];
//Table Row Controls
RowControlsController *rowControlsController = [[RowControlsControlleralloc] initWithStyle:UITableViewStylePlain];
rowControlsController.title =@"Row Controls";
rowControlsController.rowImage = [UIImageimageNamed:@"rowControlsIcon.png"];
[array addObject:rowControlsController];
[rowControlsControllerrelease];
//Move Me
MoveMeController *moveMeController = [[MoveMeControlleralloc] initWithStyle:UITableViewStylePlain];
moveMeController.title =@"Move Me";
moveMeController.rowImage = [UIImageimageNamed:@"moveMeIcon.png"];
[array addObject:moveMeController];
[moveMeControllerrelease];
//Delete Me
DeleteMeController *deleteMeController = [[DeleteMeControlleralloc] initWithStyle:UITableViewStylePlain];
deleteMeController.title =@"Delete Me";
deleteMeController.rowImage = [UIImageimageNamed:@"deleteMeIcon.png"];
[array addObject:deleteMeController];
[deleteMeControllerrelease];
//President View/Edit
PresidentViewController *presidentViewController = [[PresidentViewControlleralloc] initWithStyle:UITableViewStylePlain];
presidentViewController.title =@"Detail Edit";
presidentViewController.rowImage = [UIImageimageNamed:@"detailEditIcon.png"];
[array addObject:presidentViewController];
[presidentViewControllerrelease];
self.controllers = array;
[array release];
[superviewDidLoad];
}
-(void)viewDidUnload{
self.controllers =nil;
[superviewDidUnload];
}
-(void)dealloc{
[controllersrelease];
[superdealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.controllerscount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *FirstLevelCell = @"FirstLevelCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:FirstLevelCell];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:FirstLevelCell] autorelease];
}
//Configue the cell
NSUInteger row = [indexPath row];
SecondLevelViewController *controller = [controllersobjectAtIndex:row];
cell.textLabel.text = controller.title;
cell.imageView.image = controller.rowImage;
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark -
#pragma mark Table View Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
SecondLevelViewController *nextController = [self.controllersobjectAtIndex:row];
[self.navigationControllerpushViewController:nextController animated:YES];
}
@end
//
// SecondLevelViewController.h
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
@interface SecondLevelViewController :UITableViewController {
UIImage *rowImage;
}
@property (nonatomic,retain) UIImage *rowImage;
@end
//
// SecondLevelViewController.m
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"SecondLevelViewController.h"
@implementation SecondLevelViewController
@synthesize rowImage;
@end
//
// DisclosureButtonController.h
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"SecondLevelViewController.h"
@classDisclosureDetailController;
@interface DisclosureButtonController :SecondLevelViewController {
NSArray *list;
DisclosureDetailController *childController;
}
@property (nonatomic,retain) NSArray *list;
@end
//
// DisclosureButtonController.m
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"DisclosureButtonController.h"
#import"DisclosureDetailController.h"
@implementation DisclosureButtonController
@synthesize list;
- (void)viewDidLoad{
NSArray *array = [[NSArrayalloc] initWithObjects:@"Toy Story",@"A Bug's Life",@"Toy Story 2",@"Monsters, Inc.",@"Finding Nemo",@"The Incredibles",@"Cars",@"Ratatouille",@"WALL-E",@"Up",@"Toy Story 3",@"Cars 2",@"Brave",nil];
self.list = array;
[array release];
[superviewDidLoad];
}
- (void)viewDidUnload{
self.list = nil;
[childControllerrelease], childController = nil;
[superdealloc];
}
- (void)dealloc{
[listrelease];
[childControllerrelease];
[superdealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [list count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *DisclosureButtonCellIdentifier =@"DisclosureButtonCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DisclosureButtonCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:DisclosureButtonCellIdentifier]autorelease];
}
NSUInteger row = [indexPath row];
NSString *rowString = [list objectAtIndex:row];
cell.textLabel.text = rowString;
cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;
[rowString release];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"Hey, do you see the disclosure button?"message:@"If you're trying to drill down, touche that instead"delegate:nilcancelButtonTitle:@"Won't happen again"otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
if(childController ==nil){
childController = [[DisclosureDetailControlleralloc] initWithNibName:@"DisclosureDetail"bundle:nil];
}
childController.title =@"Disclosure Button Pressed";
NSUInteger row = [indexPath row];
NSString *selectedMovie = [list objectAtIndex:row];
NSString *detailMessage = [[NSString alloc] initWithFormat:@"You pressed the disclosure button for %@.", selectedMovie];
childController.message = detailMessage;
childController.title = selectedMovie;
[detailMessage release];
[self.navigationControllerpushViewController:childControlleranimated:YES];
}
@end
//
// DisclosureDetailController.h
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
@interface DisclosureDetailController :UIViewController {
UILabel *label;
NSString *message;
}
@property (nonatomic,retain) IBOutletUILabel *label;
@property (nonatomic,copy) NSString *message;
@end
//
// DisclosureDetailController.m
// test15
//
// Created by bear lily on 12-2-21.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"DisclosureDetailController.h"
@implementation DisclosureDetailController
@synthesize label;
@synthesize message;
-(void)viewWillAppear:(BOOL)animated{
label.text =message;
[superviewWillAppear:animated];
}
- (void)viewDidUnload{
self.label = nil;
self.message = nil;
[superviewDidUnload];
}
- (void)dealloc{
[labelrelease];
[messagerelease];
[superdealloc];
}
@end
//
// CheckListController.h
// test15
//
// Created by bear lily on 12-2-27.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"SecondLevelViewController.h"
@interface CheckListController :SecondLevelViewController {
NSArray *list;
NSIndexPath *lastIndexPath;
}
@property (nonatomic,retain) NSArray *list;
@property (nonatomic,retain) NSIndexPath *lastIndexPath;
@end
//
// CheckListController.m
// test15
//
// Created by bear lily on 12-2-27.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"CheckListController.h"
@implementation CheckListController
@synthesize list;
@synthesize lastIndexPath;
- (void)viewDidLoad{
NSArray *array = [[NSArrayalloc] initWithObjects:@"Who Hash",@"Bubba Gump Shrimp Etouffee",@"Who Pudding",@"Scooby Snackes", @"Everlasting Gobstopper",@"Green Eggs and Ham",@"Soylent Green",@"Hard Tack",@"Lembas Bread",@"Roast Beast",@"Blancmange",nil];
self.list = array;
[array release];
[superviewDidLoad];
}
- (void)viewDidUnload{
self.list = nil;
self.lastIndexPath =nil;
[superviewDidUnload];
}
- (void)dealloc{
[listrelease];
[lastIndexPathrelease];
[superdealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CheckMarkCellIdentifier =@"CheckMarkCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CheckMarkCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CheckMarkCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
NSUInteger oldRow = [lastIndexPath row];
cell.textLabel.text = [listobjectAtIndex:row];
cell.accessoryType = (row == oldRow &&lastIndexPath !=nil) ? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (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;
}
[tableViewdeselectRowAtIndexPath:indexPath animated:YES];
}
@end
//
// RowControlsController.h
// test15
//
// Created by bear lily on 12-2-29.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"SecondLevelViewController.h"
@interface RowControlsController :SecondLevelViewController{
NSArray *list;
}
@property (nonatomic,retain) NSArray *list;
- (IBAction)buttonTapped:(id)sender;
@end
//
// RowControlsController.m
// test15
//
// Created by bear lily on 12-2-29.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"RowControlsController.h"
@implementation RowControlsController
@synthesize list;
- (IBAction)buttonTapped:(id)sender{
UIButton *senderButton = (UIButton *)sender;
UITableViewCell *buttonCell = (UITableViewCell *)[senderButtonsuperview];
NSUInteger buttonRow = [[self.tableViewindexPathForCell:buttonCell] row];
NSString *buttonTitle = [list objectAtIndex:buttonRow];
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"You tapped the button"message:[NSStringstringWithFormat:@"You tapped the button for %@", buttonTitle]delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)viewDidLoad{
NSArray *array = [[NSArrayalloc] 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;
[array release];
[superviewDidLoad];
}
- (void)viewDidUnload{
self.list = nil;
[superviewDidUnload];
}
- (void)dealloc{
[listrelease];
[superdealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ControlRowIdentifier =@"ControlRowIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ControlRowIdentifier];
if(cell == nil){
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:ControlRowIdentifier];
UIImage *buttonUpImage = [UIImage imageNamed:@"button_up.png"];
UIImage *buttonDownImage = [UIImage imageNamed:@"button_down.png"];
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
button.frame =CGRectMake(0.0,0.0, buttonUpImage.size.width, buttonUpImage.size.height);
[buttonsetBackgroundImage:buttonUpImage forState:UIControlStateNormal];
[buttonsetBackgroundImage:buttonDownImage forState:UIControlStateHighlighted];
[buttonsetTitle:@"Tap"forState:UIControlStateNormal];
[buttonaddTarget:selfaction:@selector(buttonTapped:)forControlEvents:UIControlEventTouchUpInside];
cell.accessoryView = button;
}
NSUInteger row = [indexPath row];
NSString *rowTitle = [list objectAtIndex:row];
cell.textLabel.text = rowTitle;
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
NSString *rowTitle = [list objectAtIndex:row];
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@"You tapped the row."message:[NSStringstringWithFormat:@"You tappped %@", rowTitle]delegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
[alert release];
[tableViewdeselectRowAtIndexPath:indexPath animated:YES];
}
@end
//
// MoveMeController.h
// test15
//
// Created by bear lily on 12-3-1.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"SecondLevelViewController.h"
@interface MoveMeController :SecondLevelViewController {
NSMutableArray *list;
}
@property (nonatomic,retain) NSMutableArray *list;
- (IBAction)toggleMove;
@end
//
// MoveMeController.m
// test15
//
// Created by bear lily on 12-3-1.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"MoveMeController.h"
@implementation MoveMeController
@synthesize list;
- (IBAction)toggleMove{
[self.tableViewsetEditing:!self.tableView.editing animated:YES];
if(self.tableView.editing)
[self.navigationItem.rightBarButtonItemsetTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItemsetTitle:@"Move"];
}
- (void)viewDidLoad{
if(list == nil){
NSMutableArray *array = [[NSMutableArrayalloc] initWithObjects:@"Eeny",@"Meeny",@"Miney",@"Moe",@"Catch",@"A",@"Tigeer",@"By",@"The",@"Toe",nil];
self.list = array;
[array release];
}
UIBarButtonItem *moveButton = [[UIBarButtonItemalloc] initWithTitle:@"Move"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(toggleMove)];
self.navigationItem.rightBarButtonItem = moveButton;
[moveButton release];
[superviewDidLoad];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *MoveMeIdentifier = @"MoveMeIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MoveMeIdentifier];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:MoveMeIdentifier] autorelease];
cell.showsReorderControl =YES;
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listobjectAtIndex:row];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewCellEditingStyleNone;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSUInteger fromRow = [sourceIndexPath row];
NSUInteger toRow = [destinationIndexPath row];
id object = [[list objectAtIndex:fromRow] retain];
[listremoveObjectAtIndex:fromRow];
[listinsertObject:object atIndex:toRow];
[object release];
}
@end
//
// DeleteMeController.h
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"SecondLevelViewController.h"
@interface DeleteMeController :SecondLevelViewController {
NSMutableArray *list;
}
@property (nonatomic,retain)NSMutableArray *list;
- (IBAction)toggleEdit:(id)sender;
@end
//
// DeleteMeController.m
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"DeleteMeController.h"
@implementation DeleteMeController
@synthesize list;
- (IBAction)toggleEdit:(id)sender{
[self.tableViewsetEditing:!self.tableView.editinganimated:YES];
if(self.tableView.editing)
[self.navigationItem.rightBarButtonItemsetTitle:@"Done"];
else
[self.navigationItem.rightBarButtonItemsetTitle:@"Delete"];
}
- (void)viewDidLoad{
if(list == nil){
NSString *path = [[NSBundlemainBundle] pathForResource:@"computers"ofType:@"plist"];
NSMutableArray *array = [[NSMutableArrayalloc] initWithContentsOfFile:path];
self.list = array;
[array release];
}
UIBarButtonItem *editButton = [[UIBarButtonItemalloc] initWithTitle:@"Delete"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(toggleEdit:)];
self.navigationItem.rightBarButtonItem = editButton;
[editButton release];
[superviewDidLoad];
}
- (void)dealloc{
[listrelease];
[superdealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *DeleteMeCelIdentifier =@"DeleteMeCelIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DeleteMeCelIdentifier];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:DeleteMeCelIdentifier] autorelease];
}
NSInteger row = [indexPath row];
cell.textLabel.text = [self.listobjectAtIndex:row];
return cell;
}
#pragma mark -
#pragma Table View Data Source Mehods
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
[self.listremoveObjectAtIndex:row];
[tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
@end
//
// President.h
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<Foundation/Foundation.h>
#define kPresidentNumberKey @"President"
#define kPresidentNameKey @"Name"
#define kPresidentFromKey @"FromYear"
#define kPresidnetToKey @"ToYear"
#define kPresidentPartyKey @"Party"
@interface President :NSObject <NSCoding> {
int numbear;
NSString *name;
NSString *fromYear;
NSString *toYear;
NSString *party;
}
@propertyint number;
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *fromYear;
@property (nonatomic,copy) NSString *toYear;
@property (nonatomic,copy) NSString *party;
@end
//
// President.m
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"President.h"
@implementation President
@synthesize number;
@synthesize name;
@synthesize fromYear;
@synthesize toYear;
@synthesize party;
- (void)dealloc{
[namerelease];
[fromYearrelease];
[toYearrelease];
[partyrelease];
[superdealloc];
}
#pragma mark -
#pragma mark NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoderencodeInt:self.numberforKey:kPresidentNumberKey];
[aCoder encodeObject:self.nameforKey:kPresidentNameKey];
[aCoder encodeObject:self.fromYearforKey:kPresidentFromKey];
[aCoder encodeObject:self.toYearforKey:kPresidnetToKey];
[aCoder encodeObject:self.partyforKey:kPresidentPartyKey];
}
- (id)initWithCoder:(NSCoder *)aDecoder{
if(self == [superinit]){
number = [aDecoder decodeIntForKey:kPresidentNumberKey];
name = [[aDecoder decodeObjectForKey:kPresidentNameKey]retain];
fromYear = [[aDecoder decodeObjectForKey:kPresidentFromKey]retain];
toYear = [[aDecoder decodeObjectForKey:kPresidnetToKey]retain];
party = [[aDecoder decodeObjectForKey:kPresidentPartyKey]retain];
}
return self;
}
@end
//
// PresidentViewController.h
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
#import"SecondLevelViewController.h"
@interface PresidentViewController :SecondLevelViewController {
NSMutableArray *list;
}
@property (nonatomic,retain) NSMutableArray *list;
@end
//
// PresidentViewController.m
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"PresidentViewController.h"
#import"PresidentDetailController.h"
#import"President.h"
@implementation PresidentViewController
@synthesize list;
- (void)viewDidLoad{
NSString *path = [[NSBundlemainBundle] pathForResource:@"Presidents"ofType:@"plist"];
NSData *data;
NSKeyedUnarchiver *unarchiver;
data = [[NSDataalloc] initWithContentsOfFile:path];
unarchiver = [[NSKeyedUnarchiveralloc] initForReadingWithData:data];
NSMutableArray *array = [unarchiver decodeObjectForKey:@"Presidents"];
self.list = array;
[unarchiver finishDecoding];
[unarchiver release];
[data release];
[superviewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
[self.tableViewreloadData];
[superviewWillAppear:animated];
}
- (void)dealloc{
[listrelease];
[superdealloc];
}
#pragma mark -
#pragma mark Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listcount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *PresidentListCellIdentifier =@"PresidentListCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PresidentListCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:PresidentListCellIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
President *thePres = [self.listobjectAtIndex:row];
cell.textLabel.text = thePres.name;
cell.detailTextLabel.text = [NSStringstringWithFormat:@"%@ - %@" , thePres.fromYear, thePres.toYear];
return cell;
}
#pragma mark -
#pragma mark Table Delegate Methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSUInteger row = [indexPath row];
President *prez = [self.listobjectAtIndex:row];
PresidentDetailController *childController = [[PresidentDetailControlleralloc] initWithStyle:UITableViewStyleGrouped];
childController.title = prez.name;
childController.president = prez;
[self.navigationControllerpushViewController:childController animated:YES];
[childControllerrelease];
}
@end
//
// PresidentDetailController.h
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import<UIKit/UIKit.h>
@classPresident;
#define kNumberOfEditableRows4
#define kNameRowIndex0
#define kFromYearRowIndex1
#define kToYearRowIndex2
#define kPartyIndex3
#define kLabelTag4096
@interface PresidentDetailController :UITableViewController<UITextFieldDelegate>{
President *president;
NSArray *fieldLables;
NSMutableDictionary *tempValues;
UITextField *textFieldBeingEdited;
}
@property (nonatomic,retain) President *president;
@property (nonatomic,retain) NSArray *fieldLables;
@property (nonatomic,retain) NSMutableDictionary *tempValues;
@property (nonatomic,retain) UITextField *textFieldBeingEdited;
- (IBAction)cancel:(id)sender;
- (IBAction)save:(id)sender;
- (IBAction)textFieldDone:(id)sender;
@end
//
// PresidentDetailController.m
// test15
//
// Created by bear lily on 12-3-2.
// Copyright 2012年 shanghai. All rights reserved.
//
#import"PresidentDetailController.h"
#import"President.h"
@implementation PresidentDetailController
@synthesize president;
@synthesize fieldLables;
@synthesize tempValues;
@synthesize textFieldBeingEdited;
- (IBAction)cancel:(id)sender{
[self.navigationControllerpopViewControllerAnimated:YES];
}
- (IBAction)save:(id)sender{
if(textFieldBeingEdited !=nil){
NSNumber *tagAsNum = [[NSNumber alloc] initWithInt:textFieldBeingEdited.tag];
[tempValuessetObject:textFieldBeingEdited.textforKey:tagAsNum];
[tagAsNum release];
}
for (NSNumber *key in [tempValues allKeys]) {
switch ([key intValue]) {
case kNameRowIndex:
president.name = [tempValuesobjectForKey:key];
break;
case kFromYearRowIndex:
president.fromYear = [tempValuesobjectForKey:key];
break;
case kToYearRowIndex:
president.toYear = [tempValuesobjectForKey:key];
break;
case kPartyIndex:
president.party = [tempValuesobjectForKey:key];
break;
default:
break;
}
}
[self.navigationControllerpopViewControllerAnimated:YES];
NSArray *allControllers = self.navigationController.viewControllers;
UITableViewController *parent = [allControllers lastObject];
[parent.tableViewreloadData];
}
- (IBAction)textFieldDone:(id)sender{
//[sender resignFirstResponder];
UITableViewCell *cell = (UITableViewCell *)[[sendersuperview] superview];
UITableView *table = (UITableView *)[cellsuperview];
NSIndexPath *textFieldIndexPath = [table indexPathForCell:cell];
NSUInteger row = [textFieldIndexPath row];
row++;
if(row >= kNumberOfEditableRows){
row = 0;
}
NSIndexPath *newPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *nextCell = [self.tableViewcellForRowAtIndexPath:newPath];
UITextField *nextField = nil;
for (UIView *oneView in nextCell.contentView.subviews){
if( [oneView isMemberOfClass:[UITextFieldclass]]){
nextField = (UITextField *)oneView;
}
}
[nextFieldbecomeFirstResponder];
}
#pragma mark -
- (void)viewDidLoad{
NSArray *array = [[NSArrayalloc] initWithObjects:@"Name:",@"From:",@"To:",@"Party:",nil];
self.fieldLables = array;
[array release];
UIBarButtonItem *cancelButton = [[UIBarButtonItemalloc] initWithTitle:@"Cancel"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
UIBarButtonItem *saveButton = [[UIBarButtonItemalloc] initWithTitle:@"Save"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(save:)];
self.navigationItem.rightBarButtonItem = saveButton;
NSMutableDictionary *dict = [[NSMutableDictionaryalloc] init];
self.tempValues = dict;
[dict release];
[superviewDidLoad];
}
- (void)dealloc{
[presidentrelease];
[fieldLablesrelease];
[tempValuesrelease];
[textFieldBeingEditedrelease];
[superdealloc];
}
#pragma mark -
#pragma Table Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return kNumberOfEditableRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *PresidentCellIdentifier =@"PresidentCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PresidentCellIdentifier];
if(cell == nil){
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:PresidentCellIdentifier] autorelease];
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(10,10, 75, 25)];
lable.textAlignment =UITextAlignmentRight;
lable.tag =kLabelTag;
lable.font = [UIFontboldSystemFontOfSize:14];
[cell.contentViewaddSubview:lable];
[lable release];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(90,12, 200, 25)];
textField.clearsOnBeginEditing =NO;
[textField setDelegate:self];
//textField.returnKeyType = UIReturnKeyDone;
[textFieldaddTarget:selfaction:@selector(textFieldDone:)forControlEvents:UIControlEventEditingDidEndOnExit];
[cell.contentViewaddSubview:textField];
}
NSUInteger row = [indexPath row];
UILabel *lable = (UILabel *)[cell viewWithTag:kLabelTag];
UITextField *textField = nil;
for (UIView *oneView in cell.contentView.subviews){
if([oneView isMemberOfClass:[UITextFieldclass]]){
textField = (UITextField *)oneView;
}
}
lable.text = [fieldLablesobjectAtIndex:row];
NSNumber *rowAsNum = [[NSNumber alloc] initWithInt:row];
switch (row) {
case kNameRowIndex:
if([[tempValues allKeys] containsObject:rowAsNum])
textField.text = [tempValuesobjectForKey:rowAsNum];
else
textField.text =president.name;
break;
case kFromYearRowIndex:
if([[tempValues allKeys] containsObject:rowAsNum])
textField.text = [tempValuesobjectForKey:rowAsNum];
else
textField.text =president.fromYear;
break;
case kToYearRowIndex:
if([[tempValues allKeys] containsObject:rowAsNum])
textField.text = [tempValuesobjectForKey:rowAsNum];
else
textField.text =president.toYear;
break;
case kPartyIndex:
if([[tempValues allKeys] containsObject:rowAsNum])
textField.text = [tempValuesobjectForKey:rowAsNum];
else
textField.text =president.party;
break;
default:
break;
}
if(textFieldBeingEdited == textField) {
textFieldBeingEdited = nil;
}
textField.tag = row;
[rowAsNum release];
return cell;
}
#pragma mark -
#pragma mark Table Deletage Methods
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
return nil;
}
#pragma mark Text Field Delegate Methods
- (void) textFieldDidEndEditing:(UITextField *)textField{
NSNumber *tagAsNum = [[NSNumber alloc] initWithInt:textField.tag];
[tempValuessetObject:textField.textforKey:tagAsNum];
[tagAsNum release];
}
@end