胡神

导航

TableView 自定义与编辑

Movies.h   Movies.m   RootViewController.h   RootViewController.m   RootViewController.h    RootViewController.m

 

//

//  Movies.h

//  Untitled2

//

//  Created by guo gxk on 13-2-28.

//  Copyright 2013 fujian. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

 

@interfaceMovies : NSObject {

NSStringname;

NSNumbermoney;

NSStringdescripe;

}

 

@property(nonatomic,retain) NSString* name;

@property(nonatomic,retain) NSNumber* money;

@property(nonatomic,retain) NSString* descripe;

 

-(id) initWithName:(NSString*) name1 

momey:(NSNumber*) money1 

  descript:(NSString*)descripe1;

 

@end

 

 

 

 

//

//  Movies.m

//  Untitled2

//

//  Created by guo gxk on 13-2-28.

//  Copyright 2013 fujian. All rights reserved.

//

 

#import "Movies.h"

 

 

@implementationMovies

@synthesizename;

@synthesizemoney;

@synthesizedescripe;

 

-(id) initWithName:(NSString*)name1 

momey:(NSNumber*)money1 

  descript:(NSString*)descripe1{

self=[superinit];

if(!self) {

returnnil;

}

self.name=name1;

self.money=money1;

self.descripe=descripe1;

returnself;

}

 

@end

 

 

 

 

 

 

//

//  RootViewController.h

//  Untitled3

//

//  Created by guo gxk on 13-3-1.

//  Copyright 2013 fujian. All rights reserved.

//

 

#import <UIKit/UIKit.h>

#import "EditViewController.h"

#import "Movies.h"

 

@interfaceRootViewController : UITableViewController {

MoviesobjectMovies;

NSMutableArrayarray;

EditViewControllereditVC;

UITableViewCellnibLoadedCell;

}

 

@property(nonatomic,retain) NSMutableArray* array;

@property(nonatomic,retain) EditViewController* editVC;

@property(nonatomic,retain) Movies* objectMovies;

@property(nonatomic,retain)IBOutletUITableViewCell* nibLoadedCell;

 

-(void)edit;

 

@end

 

 

 

 

//

//  RootViewController.m

//  Untitled3

//

//  Created by guo gxk on 13-3-1.

//  Copyright 2013 fujian. All rights reserved.

//

 

#import "RootViewController.h"

 

NSString *testMovieData[][3]={

{@"left",@"100100",@"fly on the way"},

{@"CHYUG",@"990100",@"fyuh kungo"},

{@"GGGGG",@"7777",@"guoxinkun"},

{@"Hello",@"999900",@"how are you"},

{@"good",@"333333",@"it is good"}

};

 

 

@implementationRootViewController

 

 

@synthesizenibLoadedCell;

@synthesizearray;

@synthesizeeditVC;

@synthesizeobjectMovies;

 

-(void)LoadData{

array=[[NSMutableArrayallocinit];

for(inti=0; i<5; i++) {

Movies* object=[[Moviesallocinit];

object.name=testMovieData[i][0];

object.money=[NSNumbernumberWithInt:[testMovieData[i][1intValue]];

object.descripe=testMovieData[i][2];

[arrayaddObject:object];

[object release];

}

 

}

 

 

 

-(void)edit{

[self.navigationControllerpushViewController:editVCanimated:YES];

}

#pragma mark -

#pragma mark View lifecycle

 

 

- (void)viewDidLoad {

    [superviewDidLoad];

editVC=[[EditViewControlleralloc]initWithNibName:@"EditViewController"bundle:nil];

array=[[NSMutableArrayallocinitWithObjects:@"one",@"two",@"three",@"four",@"five",nil];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

self.navigationItem.rightBarButtonItemself.editButtonItem;

self.navigationItem.leftBarButtonItem=[[UIBarButtonItemalloc]

  initWithTitle:@"Add" 

  style:UIBarButtonItemStylePlain

  target:self 

  action:@selector(edit)];

self.navigationItem.title=@"movies";

[selfLoadData];

}

 

 

 

- (void)viewWillAppear:(BOOL)animated {

    [superviewWillAppear:animated];

    [self.tableViewreloadData];

}

 

/*

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];

}

*/

/*

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

}

*/

/*

- (void)viewDidDisappear:(BOOL)animated {

[super viewDidDisappear:animated];

}

*/

 

/*

 // Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations.

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

 */

 

 

#pragma mark -

#pragma mark Table view data source

 

// Customize the number of sections in the table view.

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return1;

}

 

 

// Customize the number of rows in the table view.

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

    return[arraycount];

}

 

 

// Customize the appearance of table view cells.

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

 

    

    staticNSString*CellIdentifier = @"Cell";

    UITableViewCell*cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil) {

     //   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

NSArray* arrayCell=[[NSBundlemainBundleloadNibNamed:@"TableViewCell" 

owner:self 

  options:NULL];

cell=[arrayCell objectAtIndex:0];

}

//Movies* movie=[[Movies alloc]init];

Movies* amovie=[arrayobjectAtIndex:indexPath.row];

UILabel* labelName=(UILabel*)[cell viewWithTag:1];

UILabel* labelMoney=(UILabel*)[cell viewWithTag:2];

UILabel* labelDes=(UILabel*)[cell viewWithTag:3];

labelName.text=amovie.name;

labelMoney.text=[NSStringstringWithFormat:@"%d",[amovie.moneyintValue]];

labelDes.text=amovie.descripe;   

// Configure the cell.

//cell.textLabel.text=[array objectAtIndex:indexPath.row];

    returncell; 

 

}

 

 

/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

*/

 

 

 

// Override to support editing the table view.

- (void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {

    

    if(editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source.

//[array removeObjectAtIndex:indexPath.row];

[arrayremoveObjectAtIndex:indexPath.row];

        [tableViewdeleteRowsAtIndexPaths:[NSArrayarrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }   

    //else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.

   // }   

}

 

 

 

/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

}

*/

 

 

/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the item to be re-orderable.

    return YES;

}

*/

 

 

#pragma mark -

#pragma mark Table view delegate

 

- (void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath {

    

/*

<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

     // ...

     // Pass the selected object to the new view controller.

[self.navigationController pushViewController:detailViewController animated:YES];

[detailViewController release];

*/

self.editVC.movie=[arrayobjectAtIndex:indexPath.row];

[self.navigationControllerpushViewController:editVCanimated:YES];

}

 

 

#pragma mark -

#pragma mark Memory management

 

- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];

    

    // Relinquish ownership any cached data, images, etc that aren't in use.

}

 

- (void)viewDidUnload {

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.

    // For example: self.myOutlet = nil;

}

 

 

- (void)dealloc {

    [superdealloc];

}

 

 

@end

 

//

//  EditViewController.h

//  Untitled3

//

//  Created by guo gxk on 13-3-1.

//  Copyright 2013 fujian. All rights reserved.

//

 

#import <UIKit/UIKit.h>

#import "Movies.h"

 

@interfaceEditViewController : UIViewController<UITextFieldDelegate> {

Moviesmovie;

UITextFieldtitleField;

UITextFieldnumberField;

UITextFieldsummaryField;

}

@property(nonatomic,retainIBOutletUITextField* titleField;

@property(nonatomic,retainIBOutletUITextField* numberField;

@property(nonatomic,retainIBOutletUITextField* summaryField;

@property(nonatomic,retain) Movies* movie;

 

-(IBAction)Done;

@end

 

//

//  EditViewController.m

//  Untitled3

//

//  Created by guo gxk on 13-3-1.

//  Copyright 2013 fujian. All rights reserved.

//

 

#import "EditViewController.h"

 

 

@implementationEditViewController

 

@synthesizemovie;

@synthesizetitleField;

@synthesizenumberField;

@synthesizesummaryField;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.

/*

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization.

    }

    return self;

}

*/

 

 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

- (void)viewDidLoad {

    [superviewDidLoad];

titleField.delegate=self;

    numberField.delegate=self;

summaryField.delegate=self;

self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]

  initWithTitle:@"Save" 

  styleUIBarButtonItemStyleDone

  target:self 

  action:@selector(Done)];

}

 

 

/*

// Override to allow orientations other than the default portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations.

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

*/

- (void)viewWillAppear:(BOOL)animated{

[superviewWillAppear:animated];

self.titleField.text=self.movie.name;

NSNumberFormatter* formatter=[[[NSNumberFormatterallocinitautorelease];

[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

self.numberField.text=[formatter stringFromNumber:self.movie.money];

self.summaryField.text=self.movie.descripe;

}

 

- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [superdidReceiveMemoryWarning];

    

    // Release any cached data, images, etc. that aren't in use.

}

 

- (void)viewDidUnload {

    [superviewDidUnload];

 

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{

[titleFieldresignFirstResponder];

    [numberFieldresignFirstResponder];

[summaryFieldresignFirstResponder];

}

 

- (void)textFieldDidBeginEditing:(UITextField*)textField{

textField.text=nil;

}

 

- (BOOL)textFieldShouldReturn:(UITextField*)textField{

[textField resignFirstResponder];

returnYES;

}

-(IBAction)Done{

self.movie.name=self.titleField.text;

NSNumberFormatter* formatter=[[NSNumberFormatterallocinit];

[formatter setNumberStyle:NSNumberFormatterCurrencyStyle];

self.movie.money=[formatter numberFromString:self.numberField.text];

[formatter release];

self.movie.descripe=self.summaryField.text;

[self.navigationControllerpopViewControllerAnimated:YES];

}

 

- (void)dealloc {

    [superdealloc];

}

 

 

@end

 

posted on 2013-03-21 22:28  胡神  阅读(196)  评论(0)    收藏  举报