ip13AppDelegate.h
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (strong, nonatomic) IBOutlet UINavigationController *navVC;
ip13AppDelegate.m
@synthesize navVC;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window makeKeyAndVisible];
self.window.rootViewController = navVC;
return YES;
}
ip13ViewController.h
#import <UIKit/UIKit.h>
@interface ip13ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
NSMutableArray *dataArr;
}
@property (nonatomic, retain) IBOutlet UIBarButtonItem *editButton, *addButton;
@property (nonatomic, retain) IBOutlet UITableView *listTblView;
- (IBAction)tableEditingButtonPressed:(UIBarButtonItem *)sender;
- (IBAction)addButtonPrtessed:(id)sender;
@end
ip13ViewController.m
#import "ip13ViewController.h"
#import "AddEditView.h"
@synthesize editButton,
addButton,
listTblView;
- (IBAction)addButtonPrtessed:(id)sender
{
AddEditView *AddEditViewVC = [[AddEditView alloc] init];
AddEditViewVC.refArr = dataArr;
AddEditViewVC.selectedIndex = -1;
[self.navigationController pushViewController:AddEditViewVC animated:YES];
}
- (IBAction)tableEditingButtonPressed:(UIBarButtonItem *)sender
{
if(!listTblView.editing)
[editButton setTitle:@"Done"];
else
[editButton setTitle:@"Edit"];
listTblView.editing = !listTblView.editing;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = editButton;
self.navigationItem.rightBarButtonItem = addButton;
dataArr = [[NSMutableArray alloc] initWithObjects:@"Apple", @"Strawberry", @"Mango", @"Grapes", @"Pineapple", @"Cherry", @"Orange", @"Watermalon", nil];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[listTblView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.textLabel.text = [dataArr objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
//NSLog(@"%d, %d", sourceIndexPath.row, destinationIndexPath.row);
NSString *sourceObj = [dataArr objectAtIndex:sourceIndexPath.row];
[dataArr removeObjectAtIndex:sourceIndexPath.row];
[dataArr insertObject:sourceObj atIndex:destinationIndexPath.row];
[tableView reloadData];
}
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
return proposedDestinationIndexPath;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[dataArr removeObjectAtIndex:indexPath.row];
[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[[NSArray alloc] initWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
AddEditView *AddEditViewVC = [[AddEditView alloc] init];
AddEditViewVC.refArr = dataArr;
AddEditViewVC.selectedIndex = indexPath.row;
[self.navigationController pushViewController:AddEditViewVC animated:YES];
}
AddEditView.h
#import <UIKit/UIKit.h>
@interface AddEditView : UIViewController
@property (nonatomic, retain) IBOutlet UITextField *fruitTxtFld;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *saveBarBtn;
@property (nonatomic, retain) NSMutableArray *refArr;
@property (nonatomic, readwrite) NSInteger selectedIndex;
- (IBAction)saveBarBtnPressed:(id)sender;
@end
AddEditView.m
@synthesize fruitTxtFld, saveBarBtn;
@synthesize refArr, selectedIndex;
- (IBAction)saveBarBtnPressed:(id)sender
{
if(selectedIndex >= 0)
{
[refArr removeObjectAtIndex:selectedIndex];
[refArr insertObject:fruitTxtFld.text atIndex:selectedIndex];
}
else
[refArr addObject:fruitTxtFld.text];
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = saveBarBtn;
if(selectedIndex >= 0)
{
fruitTxtFld.text = [refArr objectAtIndex:selectedIndex];
}
}
No comments:
Post a Comment
Comment