【问题标题】:ios/xcode/coredata: Best way to update screensios/xcode/core data:更新屏幕的最佳方式
【发布时间】:2015-04-13 19:08:36
【问题描述】:

网络程序员尝试学习 ios。我有一个我认为相当普遍的设置。表格视图、详细信息屏幕、添加新项目屏幕和编辑项目屏幕。

按照各种教程,我将数据更改保存到托管对象上下文,因此也保存在持久存储中。那部分工作正常。但是,我正在尝试使用委托模式来使屏幕反映更改并且屏幕没有更新(表格视图和详细视图都没有。)详细视图通过导航 BTW 呈现,并且添加和编辑屏幕是模态的.

更新屏幕的正确方法是什么?

我已经尝试了所有方法,因此表格视图中的以下代码只是我尝试过的一个示例...将表格视图控制器声明为委托并实现所需的方法,但我显然遗漏了一些东西:

 @interface IDTVC : UIViewController<UITableViewDataSource, UITableViewDelegate,NSFetchedResultsControllerDelegate>
    @property (strong, nonatomic) IBOutlet UITableView *tableView;
    @property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;

in .m file:

in nsfetchedresultscontroller
 self.fetchedResultsController.delegate=self;

in view will appear

[[self tableView] reloadData]; 

如果有可接受的方式或其他任何建议,感谢您清楚说明如何执行此操作。

【问题讨论】:

    标签: ios delegates nsfetchedresultscontroller


    【解决方案1】:

    由于您使用的是委托,如果您要添加一行,则可以使用-tableView:insertRowsAtIndexPath:。如果您正在编辑一行,您可以使用-tableView:reloadRowsAtIndexPath:。在委托方法中添加这些,因此当您添加某些内容时,它会触发这些方法之一。

    【讨论】:

    • 什么是委托方法?
    • 另外,目前在添加行时,会出现前 9 个。之后,会出现一个新行,但它会复制与最后一行相同的信息。换句话说,如果您创建第 1...9 行,tableview 显示 1,2,3,4,5,6,7,8,9 但之后的每一个都是 9,即 1,2,3,4,5, 6,7,8,9,9,9,9,9,9,9,9 等
    【解决方案2】:

    我试图依靠 Apple 在文档中的声明来实现 NSFetchedResultsController 协议“委托必须至少实现一个更改跟踪委托方法才能启用更改跟踪。提供一个空的控制器实现内容:够了。”

    这给出了上述不稳定的行为,即它可以工作到 9 然后停止更新。

    解决方案是使用文档中的完整代码:

    /*
     Assume self has a property 'tableView' -- as is the case for an instance of a UITableViewController
     subclass -- and a method configureCell:atIndexPath: which updates the contents of a given cell
     with information from a managed object at the given index path in the fetched results controller.
     */
    
    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
        [self.tableView beginUpdates];
    }
    
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
        atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    
        switch(type) {
            case NSFetchedResultsChangeInsert:
                [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                                withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeDelete:
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
                                 withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }
    
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
        atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
        newIndexPath:(NSIndexPath *)newIndexPath {
    
        UITableView *tableView = self.tableView;
    
        switch(type) {
    
            case NSFetchedResultsChangeInsert:
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                           withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeDelete:
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                           withRowAnimation:UITableViewRowAnimationFade];
                break;
    
            case NSFetchedResultsChangeUpdate:
                [self configureCell:[tableView cellForRowAtIndexPath:indexPath]
                      atIndexPath:indexPath];
                break;
    
            case NSFetchedResultsChangeMove:
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
                           withRowAnimation:UITableViewRowAnimationFade];
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
                           withRowAnimation:UITableViewRowAnimationFade];
                break;
        }
    }
    
    
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
        [self.tableView endUpdates];
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 1970-01-01
      • 2017-05-05
      • 2019-12-24
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多