【问题标题】:How to delete items from one table on the click on same item in another table using NSFetchedResultsController如何使用 NSFetchedResultsController 在另一个表中单击同一项目时从一个表中删除项目
【发布时间】:2012-01-04 10:21:12
【问题描述】:

我有两个表,当我检查第一个表中的一个项目时,它会添加到第二个表中,这部分工作正常。现在,当我从第一个表中取消检查时,我想从第二个表中删除相同的项目。我正在使用 CoreData

谢谢。

【问题讨论】:

    标签: ios xcode core-data nsfetchedresultscontroller


    【解决方案1】:

    如果您使用核心数据,则应从上下文和两个表中删除该对象。 确保您在表中实现了这些功能:

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

    【讨论】:

    • 我只想从第二个表中删除数据,不应该从第一个表中删除。
    • 这不是您在问题中提出的问题。请更好地解释自己,我会尽力提供帮助。
    猜你喜欢
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多