2016-03-22 4 views
0

Ich habe verschiedene Lösungen auf diesem durchgegangen, aber sie schienen nicht zu funktionieren. Ich bekomme Konsistenz Ausnahme nach Einfügen und Löschen von Operationen.
Zur Zeit habe ich reloadSection Methode als temporäre Lösung verwendet, die schreckliche Animationserfahrung gibt.
Wie insertRowsWithAnimation und deleteRowsWithAnimation verwenden, die in der richtigen Animation im folgenden Code führen werden:UITableView Animationsproblem beim Einfügen und Löschen von Zellen

- (void) imageTapped:(id)sender { 

    @try { 

    UIButton *button = sender; 
    NSInteger tag = button.tag; 
     GroupNode *group = [[dictGroups objectForKey:@"0"] objectAtIndex:tag]; 
     NSMutableArray *arr = [[CommonModel shared]GetAllGroupNodesByTreeId:group.TreeId]; 

     BOOL isVisited = NO; 
     GroupNode *nextNode; 
     for(GroupNode *gp in arr) 
     { 
      if(!isVisited) 
      { 
       if([gp.Id isEqual:group.Id]) 
       { 
        isVisited = YES; 
       } 
      } 
      else 
      { 
       nextNode = gp; 
       break; 
      } 
     } 

     if(![group.Indent isEqual:@"0"] && [nextNode.Indent intValue] == [group.Indent intValue] + 1) 
     { 
      if ([arrExpandedGroups containsObject:group.Id]) { 
       [arrExpandedGroups removeObject:group.Id]; 
       NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init]; 
       NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init]; 

       NSMutableArray *arr1 = [dictGroups objectForKey:@"0"]; 

       NSMutableArray *arrDelete = [[NSMutableArray alloc]init]; 
       BOOL isChild = YES; 

       for(int i = 0; i< arr1.count;i++) 
       { 
        GroupNode *gp = [arr1 objectAtIndex:i]; 
        if(![group.Id isEqual:gp.Id]) 
        { 
         if([arr1 indexOfObject:gp] < [arr1 indexOfObject:group]) 
         { 
          [arrDelete addObject:gp]; 
         } 
         else 
         { 
          if(([gp.Indent intValue] > [group.Indent intValue]) && !isChild) 
          { 
           [arrDelete addObject:gp]; 
          } 
          else if(([gp.Indent intValue] <= [group.Indent intValue])) 
          { 
           [arrDelete addObject:gp]; 
           isChild = NO; 
          } 
          else 
          { 
           [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]]; 
          } 
         } 
        } 
        else 
        { 
         [arrDelete addObject:gp]; 

         [dictExpand removeObjectForKey:[NSString stringWithFormat:@"%i", (int)button.tag]]; 
         button.tag = [arrDelete indexOfObject:gp]; 
         [dictExpand setObject:@"no" forKey:[NSString stringWithFormat:@"%i", (int)button.tag]]; 
         for(NSString *key in dictExpand.allKeys) 
         { 
          if(![key isEqual:[NSString stringWithFormat:@"%i", (int)button.tag]]) 
          { 
           if([group.Indent intValue] == 1) 
            [dictExpand setObject:@"no" forKey:key]; 
          } 
         } 
        } 
       } 

       [tableGroup beginUpdates]; 
       [dictGroups setObject:arrDelete forKey:@"0"]; 
       NSRange range = NSMakeRange(0, 1); 
       NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range]; 
       [tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone]; 
       [tableGroup endUpdates]; 
      } else { 

       if([group.Indent intValue] == 1) 
        [arrExpandedGroups removeAllObjects]; 
       [arrExpandedGroups addObject:group.Id]; 
       NSMutableArray *arrAdd = [NSMutableArray new]; 
       NSMutableArray *insertIndexPaths = [[NSMutableArray alloc] init]; 
       NSMutableArray *deleteIndexPaths = [[NSMutableArray alloc] init]; 

       NSMutableArray *arr1 = [dictGroups objectForKey:@"0"]; 

       BOOL isVisited = NO; 
       BOOL isMainVisited = NO; 
       int icount = 0; 
       for(GroupNode *gp in arr) 
       { 
        BOOL isPresentInArr1 = NO; 
        for(GroupNode *g in arr1) 
        { 
         if([g.Id isEqual:gp.Id] && ![group.Indent isEqual:@"1"]) 
         { 
          isPresentInArr1 = YES; 
          break; 
         } 
        } 

        if(!isVisited && (([gp.Indent intValue] == 0 || [gp.Indent intValue] == 1) || isPresentInArr1)) 
        { 
         [arrAdd addObject:gp]; 
         [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]]; 
         if([gp.Id isEqual:group.Id]) 
         { 
          isVisited = YES; 
          [dictExpand removeObjectForKey:[NSString stringWithFormat:@"%i", (int)button.tag]]; 
          button.tag = [arrAdd indexOfObject:gp]; 
          [dictExpand setObject:@"yes" forKey:[NSString stringWithFormat:@"%i", (int)button.tag]]; 

          for(NSString *key in dictExpand.allKeys) 
          { 
           if(![key isEqual:[NSString stringWithFormat:@"%i", (int)button.tag]]) 
           { 
            if([group.Indent intValue] == 1) 
             [dictExpand setObject:@"no" forKey:key]; 
           } 
          } 
         } 
        } 
        else if(isVisited) 
        { 
         if([gp.Indent intValue] > [group.Indent intValue] && ([gp.Indent intValue] == [group.Indent intValue] + 1) && !isMainVisited) 
         { 
          [arrAdd addObject:gp]; 
          [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]]; 
         } 
         else if([group.Indent intValue] > 1 && [gp.Indent intValue] == [group.Indent intValue] && !isMainVisited) 
         { 
          [arrAdd addObject:gp]; 
          [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]]; 
         } 

         else if([gp.Indent intValue] <= 1 && !isMainVisited) 
         { 
          [arrAdd addObject:gp]; 
          [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]]; 
          isMainVisited = YES; 
         } 
         else if([gp.Indent intValue] <= 1 && isMainVisited) 
         { 
          [arrAdd addObject:gp]; 
          [insertIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]]; 
         } 
         else 
         { 
          for(GroupNode *gn in arr1) 
          { 
           if([gn.Id isEqual:gp.Id]) 
            [deleteIndexPaths addObject:[NSIndexPath indexPathForRow:icount inSection:0]]; 
          } 
         } 
        } 

        icount ++; 
       } 

       [dictGroups setObject:arrAdd forKey:@"0"]; 
       [tableGroup beginUpdates]; 
       NSRange range = NSMakeRange(0, 1); 
       NSIndexSet *sectionToReload = [NSIndexSet indexSetWithIndexesInRange:range]; 
       [tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone]; 
       [tableGroup endUpdates]; 
      } 
     } 
    } 
    @catch (NSException *exception) { 

    } 
} 

Ich bin mir bewusst, das eine komplexe Logik, aber das ist, wie es funktioniert.
arr - Alle Datenbankeinträge Array
arr1 - Alle Datensätze angezeigt

numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    int count = (int)[[dictGroups objectForKey:@"0"] count]; 
    return count; 
} 

Diese Zahl ist als die Anzahl der Datensätze in Array in imageTapped Funktion immer gleich.

Antwort

0

Zuerst das Objekt von Tableview-Array löschen, dann

[table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom]; 
[table reloadData]; 
+0

Warum tun wir reload nach DeleteRowsAtIndexPaths, wenn der ganze Punkt ist nicht zu verwenden reload verwenden? – Nitish

+0

reloadData ist sicher in deleteRowsAtIndexPfads stürzt ab, wenn die Anzahl der Arrays 1 ist. –

1

Zeile einfügen in der Tabellenansicht als auch verwenden, wenn Sie es in Array hinzufügen. Sie sofort sind den ganzen Abschnitt Nachladen ... kann sein, dass ein Problem sein könnte (nicht sicher !!!)

diese hinzufügen, wenn Sie Indexpfad aktualisieren, während Zeile hinzugefügt:

[tableView insertRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

und dies beim Löschen des Index:

[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

kann dies kann helfen, und wenn nicht, dann versucht folgende Zeilen aus dem Code zu ändern:

[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationNone]; 

zu:

[tableGroup reloadSections:sectionToReload withRowAnimation:UITableViewRowAnimationFade];