繁体   English   中英

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

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

我准备了一个TableView,其中包含来自NSMutableArray的数据。 有一个选项可以编辑和重新排序行。 一切正常,直到手机关闭或后台运行了太多应用程序-Xcode中出现一条错误消息,表明由于内存不足,应用程序意外退出。 我想添加一些命令来保留并记住以前的单元顺序,因此在iPhone关闭后,用户将具有与以前相同的顺序。

以下是我用于启用重新排序的代码。 缺少什么吗? 请忍受,这只是我最初的尝试。

在此先感谢您的帮助!

马图斯

- (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;   
}

您需要确保坚持对self.Titleself.Images的更改。

如果在tableView:MoveRowAtIndexPath:toIndexPath:的末尾添加对用于保存行顺序的代码的调用,则应始终保存行顺序的任何更改。

您可以在移动一行后立即停止应用程序(通过Cmd+.或停止符号)来测试其是否正常工作。

我真的希望在这一点上可以发表评论...因此,我已经很抱歉为此写这篇文章作为答案。

您确实不应该放弃操作系统正在关闭您的应用程序的事实。 这是主要问题。 不是数据的持久性。

解决此问题之后,您可以研究离开应用程序时(使用UIApplicationDelegate协议中的一种方法或应用程序通知中的一种 )如何持久保存数据(使用Core Data或简单的plist文件)。 并在重新打开时重新加载。

暂无
暂无

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

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