2010-10-30 9 views
8

मेरे पास एक UITableView है जो CoreData के साथ संग्रहीत वस्तुओं की एक सूची दिखाता है। मैं निम्नलिखित कोड का उपयोग कर ऑब्जेक्ट को हटा सकता हूं:कोरडाटा के साथ UITableView में पंक्ति हटाने को एनिमेटिंग असर विफलता

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
if (editingStyle == UITableViewCellEditingStyleDelete) { 
    NSLog(@"Delete row"); 
    [managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

    // Save the context. 
    NSError *error; 
    if (![managedObjectContext save:&error]) { 
     /*do this gracefully one day */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    } 
    [self refreshTables]; //where refreshTables just reloads the data the table is using and calls [self.tableView reloadData]; 
} 

} 

लेकिन इसमें कोई एनीमेशन या सौंदर्य नहीं है।

जब मैं

[self refreshTables]; 

जगह

[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

साथ द्वारा चेतन करने की कोशिश मैं निम्नलिखित त्रुटि मिलती है:

Assertion failure in -[UITableView _endCellAnimationsWithContext:], >/SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:920 2010-10-30 16:46:35.717 MyApp[38226:207] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted).'

मैं में deleteRowsAtIndexPaths कोड होने की कोशिश की है कोई lu के साथ commit एडिटिंग स्टाइल कोड में विभिन्न स्थानों सीके (उदाहरण के लिए एमओसी से ऑब्जेक्ट को हटाने से पहले) लेकिन मुझे इस त्रुटि के आसपास नहीं लग रहा है।

मुझे पता है कि ऐप्पल के आईफोनकोरेडाटा रेसिपेस उदाहरण को संपादित/हटाना पंक्तियों को संभालने के लिए FetchedResultsController के लिए एक प्रतिनिधि सेट अप करके समस्या को संभालता है, लेकिन यदि संभव हो तो विकास में इस चरण में, मैं बस उन हटाए गए ऑब्जेक्ट्स को एनिमेट करने के लिए एक सरल समाधान चाहता हूं।

मैं अपने प्रबंधित ऑब्जेक्ट कॉन्टेक्स्ट से ऑब्जेक्ट को हटाने से पहले/बाद में पंक्ति को हटाने का एनीमेट कैसे कर सकता हूं?

संपादित करें: मैंने उसी त्रुटि के साथ एमओसी से आइटम को हटाने से पहले और बाद में DeleteRowsAtIndexPaths को हटाने का प्रयास किया है।

उत्तर

7

1) मैंने एक सही प्रतिनिधि स्थापित किया।

2) मैंने दृश्य में कॉल को हटा दिया [self.tableview reloadData]; जो (विचित्र रूप से) सब कुछ गड़बड़ कर रहा था (इस पोस्ट ने मुझे क्या देखना है और निकालने के लिए एक सुराग दिया: Serious Application Error in Core Data with fetchedResultsContainer)।

+0

# 2 मेरी समस्या तय की। धन्यवाद! –

-2
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     if (editingStyle == UITableViewCellEditingStyleDelete) { 
      NSLog(@"Delete row"); 

    // Delete the row first 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    //Then delete the object. 
      [managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]]; 

      // Save the context. 
      NSError *error; 
      if (![managedObjectContext save:&error]) { 
       /*do this gracefully one day */ 
       NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
       abort(); 
      } 
     } 


    } 
+0

नहीं, यह काम नहीं करता। मैंने पोस्ट करने से पहले कोशिश की थी। वही त्रुटि: इसमें सम्मिलन विफलता - [UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1261.5/UITableView.m:92 – glenstorey

0

क्या आप NSFetchedResultsController का उपयोग कर रहे हैं?
आपको यह त्रुटि मिलती है क्योंकि ऑब्जेक्ट अभी भी आपके टेबलव्यू डेटास्रोत में है।

शायद at this stage in development आप simple solution का उपयोग कर और एक NSFetchRequest से वस्तुओं के साथ NSArrays पॉप्युलेट कर रहे हैं। तो फिर यह प्रबंधित वस्तु संदर्भ


आप NSFetchedResultsController के लिए एक कैश का उपयोग कर रहे से वस्तु को निकालने के लिए व्यर्थ हो जाएगा? मैंने दस्तावेज़ में एक और नज़र डाली और पाया:

A controller thus effectively has three modes of operation, determined by whether it has a delegate and whether the cache file name is set.

No tracking: the delegate is set to nil. The controller simply provides access to the data as it was when the fetch was executed.

Memory-only tracking: the delegate is non-nil and the file cache name is set to nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes.

Full persistent tracking: the delegate and the file cache name are non-nil. The controller monitors objects in its result set and updates section and ordering information in response to relevant changes. The controller maintains a persistent cache of the results of its computation.

तो नियंत्रक का तरीका "कोई ट्रैकिंग नहीं" है। जिसका अर्थ है कि अगर वे प्रबंधित ऑब्जेक्ट से हटा दिए जाते हैं तो ऑब्जेक्ट्स को नियंत्रक से हटाया नहीं जाता है।
के कोड में just reloads the data का क्या अर्थ है? पंक्तियों को हटाने से पहले ऐसा करने का प्रयास करें।
या प्रतिनिधि काम करने के लिए आवश्यक उन 20 लाइनों को जोड़ें।

+0

मैं NSFetchedResultsController का उपयोग कर रहा हूं और बिना किसी समस्या के कोर डेटा स्रोत का उपयोग कर रहा हूं। संस-एनीमेशन कोड सफलतापूर्वक coredata ऑब्जेक्ट को हटा देता है। यह केवल एनीमेशन है जो परेशानी पैदा कर रहा है। – glenstorey

+0

@glenstorey मुझे लगता है कि मैंने पहली बार अपना प्रश्न पूरी तरह से नहीं पढ़ा, मेरा संपादन देखें। –

17

जब हम UITableView के डेटा स्रोत के रूप में NSFetchedResultsController उपयोग करें, हम समारोह tableView: commitEditingStyle: forRowAtIndexPath: में deleteRowsAtIndexPaths: withRowAnimation: आह्वान नहीं कर सकता है, जो अपवाद प्रश्न के रूप में उल्लेख फेंक देते हैं।

इस समस्या को हल करने का एक तरीका [self.tableView reloadData]controllerDidChangeContent: में प्रोटोकॉल NSFetchedResultsControllerDelegate से आक्रमण कर रहा है। यह वास्तव में हल करता है, हालांकि, अब फीड एनीमेशन हटा नहीं है।

तो, वैकल्पिक सुविधाजनक तरीका [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]controller: didChangeObject: atIndexPath: forChangeType: newIndexPath: में आक्रमण करके है।

नमूना के रूप में नीचे दिए गए कोड:


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Delete NSManagedObject 
    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath]; 
    [context deleteObject:object]; 

    // Save 
    NSError *error; 
    if ([context save:&error] == NO) { 
     // Handle Error. 
    } 
} 

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
     atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type 
     newIndexPath:(NSIndexPath *)newIndexPath 
{ 
    if (type == NSFetchedResultsChangeDelete) { 
     // Delete row from tableView. 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
           withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
+0

यह मेरे लिए काम करता है ... एक निराशाजनक "शाम को देर से" समस्या का हल जवाब! – seanicus

संबंधित मुद्दे