核心数据:删除记录时自动选择下一个表行
我正在开发一个带有分割视图控制器的核心数据驱动的 iPad 应用程序。想象一下 iPad 邮件应用程序,您就会走上正轨。当我在根视图控制器中选择一条记录时,详细信息将显示在 DetailViewController 中。
在详细信息视图上,我有一个删除按钮。单击时,它会告诉其 Core Data 上下文删除当前对象。它正确执行删除,并且该行从 RootViewController 中消失,正如它应该的那样。
如何让 RootViewController 自动选择已删除行之后的行,以便随后在详细视图中显示详细信息? (或者如果删除的行是最后一行,则自动选择上一行?)
I'm working on a Core Data driven iPad app with a split view controller. Just imagine the iPad Mail app and you'll be on the right track. When I select a record in the Root View Controller, the details display in the DetailViewController.
On the Detail View, I have a delete button. When clicked, it tells its Core Data context to delete the current object. It performs the delete correctly and the row disappears from the RootViewController, as it should.
How can I get the RootViewController to automatically select the row after the row that was deleted so it subsequently displays the details in the detail view? (Or automatically select the previous row if the deleted row was the last row?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 NSFetchedResultsController 来管理表,那么您可以使用它的委托方法来响应更改。
使用
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
委托方法并检查NSFetchedResultsChangeDelete
更改类型。您可以查看
indexPath
是否与表中当前选定的行匹配,然后进行操作。If you use an
NSFetchedResultsController
to manage the table then you can use it's delegate methods to respond to changes.Use the
controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:
delegate method and check for theNSFetchedResultsChangeDelete
change type.You can see if the
indexPath
matches the currently selected rows in your table and then act upon that.好的。我想我明白了。这就是我所做的。 (如果您看到更好的方法,请加入。)
首先,我定义了一个新的 NSInteger ivar,lastSelectedRow。
接下来,我改变了
到
然后我就改变了
到
瞧!
作为额外的好处,我现在可以在应用程序终止时存储最后选定的行,并且可以在下次启动时直接返回到它。
OK. I think I figured it out. Here's what I did. (Please chime in if you see a better way.)
First, I defined a new NSInteger ivar, lastSelectedRow.
Next, I changed
to
Then I changed
to
Und voila!!
As an added bonus I can now store the last selected row when my app is terminated, and I can go right back to it next time it launches.