Abdulrahman Jami
Abdulrahman Jami

Reputation: 113

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.

Upvotes: 1

Views: 669

Answers (1)

Thibault
Thibault

Reputation: 79

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].

Upvotes: 2

Related Questions