Saving Method for a button - ios

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.

Related

Whenever I try to save a NSUserDefualts to a NSMutableArray it crashes

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"]];

How to Save a button selection in NSUser Defaults

*
//
// DetailTableViewController.m
// BeautifulWorld
//
// Created by Mahesh on 10/27/15.
// Copyright © 2015 Mahesh. All rights reserved.
//
#import "DetailTableViewController.h"
#import "DetailTableViewCell.h"
#import "ContentViewController.h"
#import "TableViewHeader.h"
#import UIKit;
#interface DetailTableViewController ()
#end
#implementation DetailTableViewController{
NSMutableArray* selectedPlaces;
NSDictionary* place;
NSArray* places;
}
DetailTableViewCell * detailcell ;
- (void)viewDidLoad {
[super viewDidLoad];
selectedPlaces = [[NSMutableArray alloc]init];
selectedItems = [[NSMutableArray alloc] init];
//TODO:- NSUserDefaults Attempt
selectedPlaces = [NSMutableArray arrayWithArray:(NSArray *)[[NSUserDefaults standardUserDefaults] objectForKey:#"selectedPlaces"]];
if(selectedPlaces == nil){
selectedPlaces = [[NSMutableArray alloc] init];
}else{
NSLog(#"Saved to Defaults");
}
// 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;
detailcell.loveImageView.highlighted = YES;
self.navigationItem.title = _DetailTableModal[0];
//Wonders Of The World
//[[NSOperationQueue mainQueue] addOperationWithBlock:^ {
//Your code goes in here
//NSLog(#"Main Thread Code");
if ([self.navigationItem.title isEqualToString:#"Wonders Of The World"])
{
NSArray* titleArray = #[#"Pyramid at 'Chichen Itza'",
#"Christ the Redeemer",
#"Great Wall of China",
#"Machu Picchu",
#"Petra",
#"Taj Mahal",
#"Colosseum ",];
Title = [titleArray mutableCopy];
NSArray* descriptionArray = #[#"Yucatan, Mexico",
#"Rio de Janeiro - RJ, Brazil",
#"China",
#"Peru",
#"Wadi Musa,Jordan",
#"Agra,Uttar Pradesh,India",
#"Rome,Italy",];
Description = [descriptionArray mutableCopy];
NSArray* imageArray = #[#"1ChichenItza.jpg",
#"2ChristtheRedeemer.jpg",
#"3GreatWallofChina.jpg",
#"4MachuPicchu.jpg",
#"5Petra.jpg",
#"6Tajmahal.jpg",
#"7Colosseum.jpg",];
Image = [imageArray mutableCopy];
}
// }];
if ([self.navigationItem.title isEqualToString:#"Sky Scrapers"])
{
NSArray* titleArray = #[#"Burj Kalifa",
#"Eiffel Tower",
#"Empire State Building",
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
[self myValue];
self.tabBarController.tabBar.hidden = NO;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return Title.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * CellIdentifier = #"DetailCell";
detailcell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
int row = (int)[indexPath row];
detailcell.DetailTableCellTitle.text = Title[row];
detailcell.DetailTableCellDescription.text = Description[row];
detailcell.DetailTableCellImageView.image= [UIImage imageNamed:Image[row]];
if([selectedItems containsObject:indexPath]){
//high
detailcell.loveImageView.highlighted = YES;
}else{
//gray
detailcell.loveImageView.highlighted = NO;
}
// Adding Tag To The Love Button
// detailcell.loveImageView.tag = indexPath.row;
detailcell.favouriteButton.tag = indexPath.row;
// Adding Tap Guesture To Love Button
detailcell.loveImageView.userInteractionEnabled = YES;
// UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(loveImageTapped:)];
// tapGesture1.numberOfTapsRequired = 1;
// [detailcell.loveImageView addGestureRecognizer:tapGesture1];
[detailcell.favouriteButton addTarget:self action:#selector(loveImageTapped:) forControlEvents:UIControlEventTouchUpInside];
return detailcell;
}
#pragma mark Delete Rows With Animation
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView beginUpdates];
[Image removeObjectAtIndex:indexPath.row];
[Description removeObjectAtIndex:indexPath.row];
[Title removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
NSIndexPath * rowToReload = [NSIndexPath indexPathForRow:indexPath.row inSection:0];
NSArray * rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationLeft];
}
#pragma mark Love Button Tapped
-(void)loveImageTapped:(UIButton*)sender {
sender.selected = !sender.selected; //calling your method
[self saveMyValue:sender.selected];
if (sender.selected) {
[sender setBackgroundImage:[UIImage imageNamed:#"HeartSelectedSmall"] forState:UIControlStateNormal];
[selectedPlaces addObject:selectedPlaces];
}else{
[sender setBackgroundImage:[UIImage imageNamed:#"Heart"] forState:UIControlStateNormal];
}
[self.tableView reloadData];
} // Your Methods Added
// Set the value
- (void)saveMyValue:(BOOL)myValue {
[[NSUserDefaults standardUserDefaults] setInteger:[NSNumber numberWithBool:myValue ].integerValue forKey:#"myActionKeyHere"];
[[NSUserDefaults standardUserDefaults] synchronize];
} // Your Methods Added
// Get the value
- (NSInteger)myValue {
NSInteger myValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"myActionKeyHere"];
return myValue ;
}
/*
// 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 *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:#[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
}
}
*/
/*
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/
/*
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Navigation To The Detail Content View.
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"ShowDetails"]) {
ContentViewController * contentView = [ segue destinationViewController];
NSIndexPath * myindexpath = [self.tableView indexPathForSelectedRow];
int row = (int)[myindexpath row];
contentView.ContentModal = #[Title[row],Description[row],Image[row]];
}
}
#end Did The Same. Can you please explain me a bit more. What to do in cell for row at index path
*
You save it like this if its a BOOL, otherwise just change to NSInteger if you want multiple options etc:
// Set the value
- (void)saveMyValue:(BOOL)myValue {
[[NSUserDefaults standardUserDefaults] setInteger:[NSNumber numberWithBool:myValue ].integerValue forKey:#"myActionKeyHere"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// Get the value
- (NSInteger)myValue {
NSInteger myValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"myActionKeyHere"];
return myValue ;
}
#Hauzin did tell you the way with NSUserDefaults.
By the way, I have an other idea about your case.
First of all, the type of the button's selection status is BOOL type.
So we have to box the BOOL value as NSNumber value. (with [NSNumber numberWithBool:xxx])
And then, if you have a UITableView and the UITableView needs a number of button's selection status, you can save the Button selection status as a NSArray.
Then you could write a method with a NSArray type argument to create the new UITableView. (e.g: - (void)setupXXXtableViewWithButtonStatus:(NSArray *)buttonStatusArray;)
If you want to save button state in NSUserDefault you can use following code.
NSUserDefaults *udefaults=[NSUserDefaults standardUserDefaults];
if (btn.selected) {
[udefaults setBool:YES forKey:#"isSelected"];
}else{
[udefaults setBool:NO forKey:#"isSelected"];
}
To get the state of button you can use.
NSUserDefaults *udefaults=[NSUserDefaults standardUserDefaults];
BOOL isButtonSelected = [udefaults boolForKey:#"isSelected"];
Also, don't forget to call synchronize on udefaults like this.
[udefaults synchronize];
Hope, it helps.

Deleted row in tableview is reappearing when navigating back

Have friendsviewcontroller in which have uibarbuttonItem to edit friends list and other uibarbuttonitem to create groups for group chatrooms.
Have multiple segue for switching view controllers.
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Groups";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New" style:UIBarButtonItemStylePlain target:self
action:#selector(actionNew)];
self.tableView.separatorInset = UIEdgeInsetsZero;
chatrooms = [[NSMutableArray alloc] init];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if ([PFUser currentUser] != nil)
{
[self refreshTable];
}
else LoginUser(self);
}
#pragma mark - User actions
- (void)actionNew
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Create New Group" message:nil delegate:self
cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex: (NSInteger)buttonIndex=
{
if (buttonIndex != alertView.cancelButtonIndex)
{
UITextField *textField = [alertView textFieldAtIndex:0];
if ([textField.text isEqualToString:#""] == NO)
{
PFObject *object = [PFObject objectWithClassName:PF_CHATROOMS_CLASS_NAME];
object[PF_CHATROOMS_NAME] = textField.text;
[object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error == nil)
{
[self refreshTable];
}
else [ProgressHUD showError:#"Network error."];
}];
}
}
}
- (void)refreshTable
{
[ProgressHUD show:nil];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (error == nil)
{
[chatrooms removeAllObjects];
for (PFObject *object in objects)
{
[chatrooms addObject:object];
}
[ProgressHUD dismiss];
[self.tableView reloadData];
}
else [ProgressHUD showError:#"Network error."];
}];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [chatrooms count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
PFObject *chatroom = chatrooms[indexPath.row];
cell.textLabel.text = chatroom[PF_CHATROOMS_NAME];
return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:chatroom];
//[chatrooms removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
PFObject *chatroom = chatrooms[indexPath.row];
NSString *roomId = chatroom.objectId;
CreateMessageItem([PFUser currentUser], roomId, chatroom[PF_CHATROOMS_NAME]);
ChatView *chatView = [[ChatView alloc] initWith:roomId];
chatView.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:chatView animated:YES];
}
Deleted row in table view reappears when navigate back to the TableView
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:chatroom];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
Im unable to locate what piece of code i m missing or what exactly i m doing wrong.
If anyone can please point out to that.
Will appreciate so much.
Thanks.
When committing deletion, your code removes objects from chatrooms which is data source of the table view in your case, but this happens in your app's memory, the source from which chatrooms is populated does not change. Thus, speaking in MVC, the app's model state is not updated after the view's state is changed.
Every time the table view is showed up, your code populates chatrooms in refreshTable, if the model's state hasn't been changed, the code gets same list as before, thus the table view doesn't change.
EDIT: Instead of using another approach to refresh the table view, you need to think about what does your app do in this table view. If user can delete stuff in the table view, should your app update model (This model can be a local or remote database, a property list file, etc.) too? If yes, then update model when user inserts or deletes rows in the table view; well, if not, then you are asking a question that is not a problem, or maybe the table view should turn off editing.
EDIT1:
If you do need to update data, based on your code, you may need to do something like this:
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
PFObject *chatroom = [chatrooms objectAtIndex:indexPath.row];
[chatrooms removeObjectAtIndex:indexPath.row];
PFQuery *query = [PFQuery queryWithClassName:PF_CHATROOMS_CLASS_NAME];
[query deleteChatroom:chatroom];
[tableView deleteRowsAtIndexPaths:#[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
That is, you may need to implement method deleteChatroom: of class PFQuery.

Xcode : Storyboard and state preservation of the view controllers after quitting

I am new to programming in Xcode and I am having a problem in my app (a ToDo list). Everything works just fine but when the app is quit (Not minimised) the main view controller doesn't save what is on it (A problem if you have a ToDo list). Now I want to know what code I would have to implement in order to preserve the state of a view controller upon exit and where? (The app delegate or in my main view controller window)
.m file:
#implementation RHTaskListViewController
#synthesize tasks = _tasks;
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self)
{
//custom
}
return self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tasks = [[NSMutableArray alloc] init];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.tableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
//Segue from Add task to task list
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"AddTaskSegue"])
{
UINavigationController *navCon = segue.destinationViewController;
RHAddTaskViewController *addTaskViewController = [navCon.viewControllers objectAtIndex:0];
addTaskViewController.taskListViewController = self;
}
else if ([segue.identifier isEqualToString:#"EditDoneTaskSegue"] || [segue.identifier isEqualToString:#"EditNotDoneTaskSegue"])
{
RHEditTaskViewController *editTaskViewController = segue.destinationViewController;
editTaskViewController.task = [self.tasks objectAtIndex:self.tableView.indexPathForSelectedRow.row];
}
}
//Segue from Add task to task list
//Move Items
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
RHTask *movedTask = [self.tasks objectAtIndex:fromIndexPath.row];
[self.tasks removeObjectAtIndex:fromIndexPath.row];
[self.tasks insertObject:movedTask atIndex:toIndexPath.row];
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//Move Items
//Delete Items
-(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[self.tasks removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert)
{
}
}
//Delete Items
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tasks.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *NotDoneCellIdentifier = #"NotDoneTaskCell";
static NSString *DoneCellIdentifier = #"DoneTaskCell";
RHTask *currentTask = [self.tasks objectAtIndex:indexPath.row];
NSString *cellIdentifier = currentTask.done ? DoneCellIdentifier : NotDoneCellIdentifier;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//Saving the tasks
//[[NSUserDefaults standardUserDefaults] setString:saveTask forKey:#"taskSaved"];
//Saving the tasks
cell.textLabel.text = currentTask.name;
return cell;
}
#pragma mark - IBActions
-(void)editButtonPressed:(id)sender
{
self.editing = !self.editing;
}
#end
.h file:
#import <UIKit/UIKit.h>
#interface RHTaskListViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *tasks;
-(IBAction)editButtonPressed:(id)sender;
#end
U can use NSUserDefaults to achieve your goal.
Take this tutorial link
Main code:
For saving:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"key1" forKey:#"todo1"];
[defaults synchronize]; // do NOT forget this line!
For retrieving:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *todo1 = [defaults objectForKey:#"key1"];
Just keep in mind , u can save NSArray, NSDictionary, NSString, NSNumber and all object that conforms to NSCoping protocol.
As gran33 already noted you can use NSUserDefaults to store your app data.
If your todo list more complex than just a list of tasks (i.e. each task has more properties like time,description,location etc...) I recommend using core data to store your data. Take a look here for a CoreData tutorial.

Add view from table view cell

I know this question has been asked before but mine is different. My app has an add button and edit button that deletes/adds table views. I want every cell that is created by the user to go to the same view. I've been looking everywhere for the code but I can't find it. BTW the ____ is just a placeholder. The table coding is in the app delegate and I have a second view controller for the view that is loaded when a row is clicked.
AppDelegate.h
#interface _____AppDelegate : NSObject <UIApplicationDelegate> {
CustomCellViewController *customCellViewController;
IBOutlet UIWindow *window;
IBOutlet UITableViewCell *customCell;
NSMutableArray *data;
IBOutlet UITableView *mainTableView;
IBOutlet UINavigationItem *navItem;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#property (nonatomic, retain) CustomCellViewController *customCellViewController;
- (IBAction)addRowToTableView;
- (IBAction)editTable;
- (NSString *)dataFilePath;
#end
AppDelegate.m
#import "______AppDelegate.h"
#implementation ______AppDelegate;
#synthesize window;
#synthesize navController=_navController;
#synthesize customCellViewController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSArray *archivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[self dataFilePath]];
if (archivedArray == nil) {
data = [[NSMutableArray alloc] init];
} else {
data = [[NSMutableArray alloc] initWithArray:archivedArray];
}
// Override point for customization after application launch
self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];
return YES;
}
- (IBAction)addRowToTableView {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Product" message:#"What is the name of your product?" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok", nil];
[alert addTextFieldWithValue:#"" label:#"Name of product..."];
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeURL;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
[alert show];
}
-(void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
UITextField *tf = [alert textFieldAtIndex:0];
[data addObject:tf.text];
[self saveData];
[mainTableView reloadData];
}
}
- (IBAction)editTable {
UIBarButtonItem *leftItem;
[mainTableView setEditing:!mainTableView.editing animated:YES];
if (mainTableView.editing) {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(editTable)];
} else {
leftItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(editTable)];
}
navItem.rightBarButtonItem = leftItem;
[self saveData];
[mainTableView reloadData];
}
- (IBAction)endText {
}
- (NSInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifer = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:"Cell"] autorelease];
}
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
- (NSString *)dataFilePath {
NSString *dataFilePath;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
dataFilePath = [[documentDirectory stringByAppendingPathComponent:#"applicationData.plist"] retain];
return dataFilePath;
}
- (void)saveData {
[NSKeyedArchiver archiveRootObject:[data copy] toFile:[self dataFilePath]];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[data removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[data objectAtIndex:fromIndexPath.row] retain];
[data removeObject:item];
[data insertObject:item atIndex:toIndexPath.row];
[item release];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)dealloc {
[window release];
[_navController release];
[customCellViewController release];
[super dealloc];
}
#end
I don't mean to be harsh, but you have a lot of basics to learn. ARC will make your life much easier and your code better, by eliminating the need to manually manage memory (for the most part). You can enable it when you first start a project. You should.
Why is an App Delegate managing a table view? No, no no. The app delegate is supposed to respond to system-level events, not run your whole application. You need a separate view controller. Find some tutorials around the web and see how a basic app using a table view is structured. There are many. My favorites are on raywenderlich.com

Resources