简体   繁体   English

UITableView中重新排序的单元格保持相同顺序

[英]Reordered cells in UITableView to remain in the same order

I prepared a TableView containing data from NSMutableArray. 我准备了一个TableView,其中包含来自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. 一切正常,直到手机关闭或后台运行了太多应用程序-Xcode中出现一条错误消息,表明由于内存不足,应用程序意外退出。 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. 我想添加一些命令来保留并记住以前的单元顺序,因此在iPhone关闭后,用户将具有与以前相同的顺序。

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 . 您需要确保坚持对self.Titleself.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. 如果在tableView:MoveRowAtIndexPath:toIndexPath:的末尾添加对用于保存行顺序的代码的调用,则应始终保存行顺序的任何更改。

You can test that this is working by stopping the app (either by Cmd+. or the stop sign) immediately after you move a row. 您可以在移动一行后立即停止应用程序(通过Cmd+.或停止符号)来测试其是否正常工作。

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 ). 解决此问题之后,您可以研究离开应用程序时(使用UIApplicationDelegate协议中的一种方法或应用程序通知中的一种 )如何持久保存数据(使用Core Data或简单的plist文件)。 And loading it back when reopening it. 并在重新打开时重新加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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