I have a alert view with a textfield so whenever someone types in it and pressed the save button it puts in the table view. when you click on the cell after being saved it takes you to a different view. Now when you go back to the home page, the cells disappear. I have tried multiple ways of figuring it out, yet still haven't been able to. Do I need to add a plist so every time I add a cell it gets saved to the plist and if so where would i start?
This code is in my table view controller
- (IBAction)add:(id)sender {
NSLog(#"%#",tableData);
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"My Favs" message:#"Hello" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.enablesReturnKeyAutomatically = YES;
alertTextField.placeholder = #"example";
[alert show];
return;
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSLog(#"%#",tableData);
//Only do the following action if the user hits the ok button
if (buttonIndex == 1){
NSString *tapTextField = [alertView textFieldAtIndex:0].text;
if (!tableData)
{
tableData = [[NSMutableArray alloc]init];
}
[tableData insertObject:tapTextField atIndex:0];
[myTableView reloadData];
}
}
I think tableData is getting deallocated when you are coming back to the home page. If the tableData is not a huge array you can store it in NSUserDefaults. This way when ever you come back to the table you can always retreat the data. Check out this code. It will store the tableData in the NSUserDefaults every time you add anything to the array. Let me know if this works for you.
#import "ViewController.h"
#interface ViewController () <UIAlertViewDelegate,UITableViewDataSource,UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *myTableView;
#property (nonatomic,strong) NSMutableArray *tableData;
#end
#implementation ViewController
-(NSMutableArray *)tableData
{
if(!_tableData){
NSMutableArray *data = [[NSUserDefaults standardUserDefaults]objectForKey:#"data"];
if(!data){
_tableData = [[NSMutableArray alloc]init];
}else{
_tableData = [data mutableCopy];
}
}
return _tableData;
}
- (IBAction)add:(UIButton *)sender {
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"My Favs" message:#"Hello" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Save", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField * alertTextField = [alert textFieldAtIndex:0];
alertTextField.enablesReturnKeyAutomatically = YES;
alertTextField.placeholder = #"example";
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 1){
NSString *tapTextField = [alertView textFieldAtIndex:0].text;
[self.tableData insertObject:tapTextField atIndex:0];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.tableData forKey:#"data"];
[defaults synchronize];
[self.myTableView reloadData];
}
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return[self.tableData count];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"dataCell" forIndexPath:indexPath];
if(self.tableData.count > 0){
cell.textLabel.text = self.tableData[indexPath.row];
}
return cell;
}
Related
I've been using NSUserDefualts. Whenever I try to save to a NSMutableArray it crashes the app. How would I fix this?
I tried to figure it out online, but it's been bringing up either swift or guides that don't support any of what I searched.
#import "scoutViewViewController.h"
#interface scoutViewViewController ()
#property (weak, nonatomic) IBOutlet UITableView *scoutView;
#end
BOOL check = YES;
#implementation scoutViewViewController
*tableData; // u need this for a standalone/ static one
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tableData =[[NSUserDefaults standardUserDefaults] objectForKey:#"data_0"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
//segue select
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"show" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"show"]) {
}
}
//ediatable tableView
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableData removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self scoutView];
}
}
- (IBAction)addCell:(id)sender {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Team Name Or Number" message: #"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//only accept if the user hit ok
// need to implement "UIAlertController" becuase UIAlert is no longer used.
if(buttonIndex == 1){
NSString *temptxtfield = [alertView textFieldAtIndex:0].text;
if(!tableData){
tableData = [[NSMutableArray alloc]init];
}
if([temptxtfield isEqual: #""] || [temptxtfield isEqual: #" "]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Team Name Or Number" message: #"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
check = NO; //check, nothing to do wiht the implemtaiont
}else{
check = YES;//check, nothing to do wiht the implemtaiont
}
if(check == YES){//check, nothing to do wiht the implemtaiont
[tableData insertObject:temptxtfield atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.scoutView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
check= NO;//check, nothing to do wiht the implemtaiont
}//check, nothing to do wiht the implemtaiont
//check is to prevent nothing to be placed into the tabel, nothing to do with how the data is inserted into the table
}
}
- (IBAction)saveTable:(id)sender {
[[NSUserDefaults standardUserDefaults] setObject: tableData forKey:#"data_0"];
}
-(void)save2{
}
#end
It gives me a SIGBART error when it crashes, I tried figuring out with break points but it's being a pain.
ended up figuring it out, pretty simple now that I think of it and I feel pretty dumb not knowing it beofore lol...
tableData = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"tabelSave"]];
Hi I am trying to trigger events for UIsegment control inside the collection view.
here is my code.
CollectionViewCell.h
#property (strong, nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;
ViewController.m
{
NSInteger selectedSegment;
}
- (UIView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
cell.mySegmentedControl.tag = indexPath.row;
selectedSegment = cell.mySegmentedControl.selectedSegmentIndex;
[cell.mySegmentedControl addTarget:self action:#selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
}
- (void) segmentValueChanged: (UISwitch *) sender {
//NSInteger index = sender.tag;
if(selectedSegment == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"!Alert"
message:#"Do you think this property is not exists?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[alert show];
}
else
{
//your code
}
}
The above code not works for me.Any help will be appreciated.
is not UISwitch it is UISegmentedControl do like
- (void) segmentValueChanged: (UISegmentedControl *) sender {
//NSInteger index = sender.tag;
if(sender.selectedSegmentIndex == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"!Alert"
message:#"Do you think this property is not exists?"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Yes", nil];
[alert show];
}
else
{
//your code
}
}
I'm a beginner in iOS. I'm performing a swipe delete option. I want to display an alert view before deleting the row. How can i perform this action.
- (void)tableView:(UITableView *)tableView commitEditingStyle:
(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"%#",collisionsArray);
if (editingStyle == UITableViewCellEditingStyleDelete)
{
NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
NSString *userId = [userinfo valueForKey:#"user_id"];
if(userId!=nil)
{
NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
collisionId = [NSString stringWithFormat:#"%#",[dict valueForKey:#"collisionId"]];
NSLog(#"%#",collisionId);
// removes saved datas from database
BOOL result = [database removeCollisionDetails:collisionId:#"accident_report"];
if(result)
{
[[SHKActivityIndicator currentIndicator]
displayCompleted:NSLocalizedString(#"val_sucess_vehicle", nil)];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[[SHKActivityIndicator currentIndicator]
displayCompleted:NSLocalizedString(#"val_error", nil)];
}
}
}
[self.tableView reloadData];
}
For this you can just display an alet view in:
if (editingStyle == UITableViewCellEditingStyleDelete){
// Show your alert view
// Set its delegate to self
}
Now you have to do something like:
#pragma mark ---- Delegate for alertview ----
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSUserDefaults *userinfo = [NSUserDefaults standardUserDefaults];
NSString *userId = [userinfo valueForKey:#"user_id"];
if(userId!=nil)
{
NSDictionary* dict = [collisionsArray objectAtIndex:indexPath.section];
collisionId = [NSString stringWithFormat:#"%#",[dict valueForKey:#"collisionId"]];
NSLog(#"%#",collisionId);
// removes saved datas from database
BOOL result = [database removeCollisionDetails:collisionId:#"accident_report"];
if(result)
{
[[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(#"val_sucess_vehicle", nil)];
[self.navigationController popViewControllerAnimated:YES];
}
else
{
[[SHKActivityIndicator currentIndicator] displayCompleted:NSLocalizedString(#"val_error", nil)];
}
}
}
-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
if (editingStyle == UITableViewCellEditingStyleDelete) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Warring" message:#"Are You Sure?" delegate:self cancelButtonTitle:#"No" otherButtonTitles:#"Yes ", nil];
[alert show];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) {
}else if (buttonIndex == 1){
NSIndexPath *indexPath=[_UserTableView indexPathForSelectedRow];
[showUData removeObjectAtIndex:indexPath.row]; //showUData NSMutableArray
[_UserTableView reloadData];
[storeData setObject:showUData forKey:#"SendData"]; //storeData NSUserDefault
[storeData synchronize];
}
}
Use UIAlertView and only delete if the user confirms. Update your code to :
if (editingStyle == UITableViewCellEditingStyleDelete)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hello World!"
message:#"Are you sure ?"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
Implement the UIAlertViewDelegate and move the original delete code to the delegate method where you detect which button has been tapped.
Hi i am trying to make a tableview with mailbox style panning http://www.mailboxapp.com/ for that i am using this library https://github.com/gloubibou/HHPanningTableViewCell and it is working fine, i swipe the cell and it moves just fine, the problem is that i want to trigger a custom action when i swipe the cell and i have only been able to do it when it is open and then i touch it.
This is the code where the action is happening
#import "TableViewController.h"
#import "HHPanningTableViewCell.h"
#interface TableViewController ()
#property (nonatomic, retain) NSArray *rowTitles;
#end
#implementation TableViewController
#pragma mark -
#pragma mark Initialization
- (id)init
{
self = [super initWithNibName:#"TableViewController" bundle:nil];
if (self != nil) {
self.rowTitles = [NSArray arrayWithObjects:#"Pan direction: None", #"Pan direction: Right", #"Pan direction: Left", #"Pan direction: Both", #"Custom trigger", nil];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// 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;
}
#pragma mark -
#pragma mark Accessors
#synthesize rowTitles = _rowTitles;
#pragma mark -
#pragma mark Rotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.rowTitles count] * 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSInteger directionMask = indexPath.row % 5;
if (cell == nil) {
cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];
// dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
// Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dark_dotted"]];
cell.drawerView = drawerView;
}
if (directionMask < 3) {
cell.directionMask = directionMask;
}
else {
cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;
if (directionMask == 4) {
cell.delegate = self;
}
}
cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];
return cell;
}
- (void)gestureRecognizerDidPan:(UIPanGestureRecognizer*)gestureRecognizer{
}
#pragma mark -
#pragma mark Table view delegate
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger directionMask = indexPath.row;
NSString *celda = [NSString stringWithFormat:#"%d", directionMask];
[cell isKindOfClass:[HHPanningTableViewCell class]];
HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;
if (directionMask == 1) {
if (HHPanningTableViewCellDirectionRight) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
if ([panningTableViewCell isDrawerRevealed]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"2"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark -
#pragma mark HHPanningTableViewCellDelegate
- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"You triggered a custom action"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
#end
I know i could use a Gesture recognizer to trigger the action but i think the library is already doing that.
in this part i trigger an action knowing exatly the cell, where was it paned to and if the back of the cell is revealed or not, but always by clicking it since it is a select function.
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSInteger directionMask = indexPath.row;
NSString *celda = [NSString stringWithFormat:#"%d", directionMask];
[cell isKindOfClass:[HHPanningTableViewCell class]];
HHPanningTableViewCell *panningTableViewCell = (HHPanningTableViewCell*)cell;
if (directionMask == 1) {
if (HHPanningTableViewCellDirectionRight) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
if ([panningTableViewCell isDrawerRevealed]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"1"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"2"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
return indexPath;
}
and i believe this other part is where the custom action should be trigered but the program never enters this function
- (void)panningTableViewCellDidTrigger:(HHPanningTableViewCell *)cell inDirection:(HHPanningTableViewCellDirection)direction
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Custom Action"
message:#"You triggered a custom action"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
I hope i made myself clear and thank you in advance.
Change the delegate method to
- (void)panningTableViewCell:(HHPanningTableViewCell *)cell didTriggerWithDirection:(HHPanningTableViewCellDirection)direction;
For the delegate method to trigger you need to set your controller as delegate for the cell. Currently in your cellForRowAtIndexPath the controller is assigned as delegate only when directionMask is 4. So you either set directionMask to be 4 in your current code (which is returning a value based on the cell position instead) or you set the controller as delegate in every case, as I've done below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
HHPanningTableViewCell *cell = (HHPanningTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSInteger directionMask = indexPath.row % 5;
if (cell == nil) {
cell = [[HHPanningTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
UIView *drawerView = [[UIView alloc] initWithFrame:cell.frame];
// dark_dotted.png obtained from http://subtlepatterns.com/dark-dot/
// Made by Tsvetelin Nikolov http://dribbble.com/bscsystem
drawerView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"dark_dotted"]];
cell.drawerView = drawerView;
}
if (directionMask < 3) {
cell.directionMask = directionMask;
}
else {
cell.directionMask = HHPanningTableViewCellDirectionLeft + HHPanningTableViewCellDirectionRight;
// previous code
//if (directionMask == 4) {
// cell.delegate = self;
//}
}
cell.delegate = self;
cell.textLabel.text = [self.rowTitles objectAtIndex:directionMask];
return cell;
}
this is my code in my .m file
#interface HomeWorkViewController ()
#end
#implementation HomeWorkViewController
#synthesize adView;
#synthesize myTableView, numbers;
-(void) viewDidLoad
{
adView.delegate=self;
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// check here if key exists in the defaults or not, if yes the retrieve results in array
if([[NSUserDefaults standardUserDefaults] objectForKey:#"numberArray"] != nil) {
self.numbers = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"numberArray"]];
}
//Register for the notification when user go to background or minimize the app, just save the array objects in the defaults
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(appWillGoToBackground:)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
//Add the Add button
UIBarButtonItem * addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target: self action: #selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[self.myTableView setEditing:editing animated:animated];
}
-(void)appWillGoToBackground:(NSNotification *)note {
NSLog(#"terminate");
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:self.numbers forKey:#"numberArray"];
[defaults synchronize];
}
-(void)insertNewObject{
//Display a UIAlertView
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Enter HomeWork" message: #"" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
//Only perform the following actions if the user hits the ok button
if (buttonIndex == 1)
{
NSString * tmpTextField = [alertView textFieldAtIndex:0].text;
if(!self. numbers){
self.numbers = [[NSMutableArray alloc]init];
}
[self.numbers insertObject:tmpTextField atIndex:0];
NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.myTableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.numbers.count;
}
-(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];
}
cell.textLabel.text = [self.numbers objectAtIndex:indexPath.row];
return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//remove our NSMutableArray
[self.numbers removeObjectAtIndex:indexPath.row];
//remove from our tableView
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
adView.hidden=FALSE;
NSLog(#"Has ad, showing");
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
adView.hidden=TRUE;
NSLog(#"Has no ads, hiding");
}
-(void)dealloc
{
[adView release];
[super dealloc];
}
#end
I have a saving method there but I want to save everything that I changed in the table by clicking a button. How do i do that?
I want to put a toolbar with a button that says back to go to the home screen, and link this button to save everything that was done to the table like delete, switch order and add.
You're almost there :)
You are using the numbers array as the data model for the table.
Make sure that this array is always updated when the table is manipulated. (For example, you need to reorder the numbers array in moveRowAtIndexPath. Currently, you do nothing in that method)
To save the model using a button, just create a UIButton in the Interface Builder and connect it to the following action:
- (IBAction)saveButtonWasPressed:(id)sender {
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:self.numbers forKey:#"numberArray"];
[defaults synchronize];
}
Maybe you also want to pop the tableviewcontroller from the navigationcontroller stack if you also want to leave the controller.