简体   繁体   中英

Reordered cells in UITableView to remain in the same order

I prepared a TableView containing data from NSMutableArray. There is an option to edit and reorder rows. All works fine, until the phone is shut down or too many applications are running in the background - there is an error message in Xcode that the application exited unexpectedly due to memory pressure. I would like to add some command to remain and remember previous cell order so after iPhone shut down user will have the same order as before.

Below is the code I am using for enabling reorder. Is there anything missing? Please bear with me, these are just my initial developing attempts.

Thanks in advance for any help!

Matus

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:         (NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {

    [_Title removeObjectAtIndex:indexPath.row];
    [_Images removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [self.Title objectAtIndex:fromIndexPath.row];
[self.Title removeObject:item];
[self.Title insertObject:item atIndex:toIndexPath.row];

NSString *image = [self.Images objectAtIndex:fromIndexPath.row];
[self.Images removeObject:image];
[self.Images insertObject:image atIndex:toIndexPath.row];
}
- (void)tableView:(UITableView *)tableView didEndReorderingRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]
              withRowAnimation:UITableViewRowAnimationNone];
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;   
}

You need to make sure that you're persisting the changes to self.Title and self.Images .

If you add a call to the code you use to save the order of your rows at the end of tableView:MoveRowAtIndexPath:toIndexPath: then any change in the order of the rows should always be saved.

You can test that this is working by stopping the app (either by Cmd+. or the stop sign) immediately after you move a row.

I really wish I could comment at this point... So, I already apologize for writing this as an answer.

You really shouldn't discard the fact that your app is being shut down by the OS. This is the main issue. Not the persistence of the data.

After you fix that, you can then look into how to persist your data (using Core Data, or a simple plist file) when leaving the app (using either one of the method in the UIApplicationDelegate protocol , or one of the application notifications ). And loading it back when reopening it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM