Core Data - Reordering TableView won't save - ios

.
Hello,
I have the following code to set the re-ordering of the tableView.
#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
// Process the row move. This means updating the data model to <span id="IL_AD6" class="IL_AD">correct</span> the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [arr objectAtIndex:fromIndexPath.row];
[arr removeObject:item];
[arr insertObject:item atIndex:toIndexPath.row];
}
But when I reload the TableView (note not a TableViewController) The order is set back to what it was before I changed anything.
Now when I setup the editing for this view (the delete buttons) I had to add this line:
[self.mainTableView setEditing:YES animated:YES];
before it appeared in my view.
Is there a similar thing I need to set with the re-ordering of the table view for it to save?
The data is fetched from a Core Data database.

In order to persist the order of the objects I'm afraid you will need to add an attribute for that to your data model. Just use an int / [NSNumber] and recompute them in a loop when a table view row is moved.

When you reload the data, are you re-populating that array with the result of a fetch from your Core Data entity? If so, the changes you made to the array (which I assume is the data source for the TV) will be blown away. If you want those changes to stick, you would need to update the model somehow to make sure the re-population of the array results in the array being indexed the way you need.

Related

Reorder NSArray based on UITableView Edit

I have an app filled with over 500 songs for churches. I am adding a new feature, which will allow them to create a "slide show" of manually chosen songs. The way the layout is now is that they have a UITableView of all the songs, and tapping a song adds that PPT's file location to an NSArray, and adds a checkmark to the row. Following this, they can click the Preview button, which will give them a tableview of just the songs they have chosen, in the order they chose it.
I would like to allow editing mode to delete songs, or reorder them, but am not sure how to have that also reorder the NSArray, so that item 0 will be the first song they have on the tableview, not just the first one they tapped.
I assume you have an array of songs, called songsArray which is used to populate table view. Then when reordering happens, add this method.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSObject *songToMove = [self.songsArray objectAtIndex:sourceIndexPath.row];
[self.songsArray removeObjectAtIndex:sourceIndexPath.row];
[self.songsArray insertObject:stringToMove atIndex:destinationIndexPath.row];
}
Dont forget to implement canMoveForRowAtIndexPath
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
This lets user to reorder all rows in your table view, if you don't want to let user to reorder specific rows, add check for indexPath.row and return NO for that row.

How to Save UITableViewController cells after rearrange to NSUserDefaults

I've an iOS application developed using Objective-C & Xcode 6.4
Right know I'm working on manually rearranging the UITableViewController cells, everything working great. But after I press the bar button EDIT and the "3 underlines" appear to drag the cell anywhere I want in the UITableViewController, (( I can't save what I did )). So how could I do a Persistent save the changes done to the table cells location ?? I mean, How to save The new rearranged NSMutableArray to a Property list -NSUserDefaults-.
I'm using a Mutable Array to display the table's cells and these methods below:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
FileWML *fileWML = [self.filesWML objectAtIndex:sourceIndexPath.row];
[self.filesWML removeObjectAtIndex:sourceIndexPath.row];
[self.filesWML insertObject:fileWML atIndex:destinationIndexPath.row];
}
PLUS would someone tell me how I can make the edit button display the word Done, while editing ?
(( I can't save what I did )) means: after I do the rearranging order I want, and then I go to the home view then I get back to the table view I edit and rearrange, the rearranging order I did get back to the default order. So, all the rearranging I did is gone.
Thanks and every help is appreciated.
You have to load your datas from your file in an array. This array will be your tableView dataSource.
When a row is moved, in the delegate method you have to change the object's place in the array. Your array should always be the same as your tableview !
Once the editing of the tableview is done you save the array in the file. The previous content in file should be erased. Then you reload your datas ([tableView reloadData].

xcode - delete a row from table view

I want to delete a row in my table view cell. At the moment, the code will look like this:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
// Remove the row from data model
[listOfAllOpenChats removeObjectAtIndex:indexPath.row];
[tableView reloadData];
}
The problem: I will become an error:
No visible #infterface "NSArray" declares the selector
"removeObjectAtIndex:"
Replace your NSArray listOfAllOpenChats with NSMutableArray,you cannot remote object from NSArray
Your listOfAllOpenChats is probably a NSArray type, which has NOT support to remove or add objects. Change your listOfAllOpenChats array type to NSMutableArray and your problem will be gone.
You can't add or remove objects from an NSArray(immutable). You have to use an NSMutableArray.

Re-arrange Rows in UITableView; Save to Core Data

I have created a list of items in the UITableView, in which, after editing, like re-arranging the rows, when clicked on Done, the editing will stop and will show the changes.
When I leave the app and comes back again when re-launching the application, it should show the changed rows that has been changed previously, before closing application.
Also, I am actually using Core Data and wondering what is needed to it.
EXAMPLE (BEFORE):
A
B
C
EXAMPLE (AFTER RE-ARRANGED):
B
C
A
SHOULD APPEAR WHEN APP IS CLOSED AND LAUNCH:
B
C
A
HOWEVER, IT SHOWS AS (LAST SAVED DATA):
A
B
C
Here are my following codes:
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = self.modules[sourceIndexPath.row];
[self.modules removeObjectAtIndex:sourceIndexPath.row];
[self.modules insertObject:stringToMove atIndex:destinationIndexPath.row];
}
-(IBAction)btnEdit:(UIBarButtonItem *)sender {
UIBarButtonItem * button = ((UIBarButtonItem*)sender);
if (!self.tableView.editing)
{
[self.tableView setEditing:YES animated:YES];
[button setTitle:#"Done"];
}
else
{
[button setTitle:#"Edit"];
[self.tableView setEditing:NO animated:YES];
}
}
THANKS!
Assuming that self.modules are your items stored in core data, adding an order field would allow you to persist the order in which you can retrieve your objects later.
You would need to load your objects ordered by this new field then update it when user rearrange the order in editing mode.
I guess you need to check where self.modules is getting set.
Is the change you are making in the sequence persisted?
If no, then this can only be achieved by - State Preservation and Restoration.

drag and drop with core data

The code below implements drag and drop functionality in a table view. As you can see it accomplishes reordering by removing the object from an array (dataList) at a certain index and then positions it at the desired index. This code doesn't use core data. My question is, how would you achieve this using core data. For example, would you continue using an array as in the code below and then save the array to core data and you try to retrieve and order the objects from core data according to the order in the array. Or would you reorder the objects directly in the core data store? if the latter, how?
#pragma mark Row reordering
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath {
NSString *item = [[dataList objectAtIndex:fromIndexPath.row] retain];
[dataList removeObject:item];
[dataList insertObject:item atIndex:toIndexPath.row];
[item release];
}
I've implemented a similar behavior by storing the order information in the coredata object. Let's call it position.
By this you are able to pass all your objects as an NSArray ordered by NSSortDescriptor with the property position.
After a drag&drop happened, make sure you update the position properties of the changed items accordingly.

Resources