2016-11-17 7 views
0

Ich habe ein Array, das NSIndexPath enthält, und ich möchte alle Objekte entfernen, die das gleiche IndexPath.Row haben. Mein aktueller Code hat einige Probleme, nicht alle Objekte mit derselben Zeile werden entfernt. Mein Code ist:Objective-C Entfernen von Objekten aus NSArray mit Indexpfad

rowValue=(int)btn.tag; 
for (int i=0; i<[SingletonClass singleton].arraySubMenuItems.count; i++) 
{ 
    NSIndexPath * Path = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]; 
    int section = (int) Path.section; 
    if (section == rowValue) 
    { 

     NSIndexPath *indexPath = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]; 
     [[SingletonClass singleton].arraySubMenuItems removeObjectAtIndex:i]; 

    } 
} 
+0

Sie iterieren und modifizieren gleichzeitig Ihr Array (insbesondere das Entfernen von Elementen). – Larme

+0

Ja, ich weiß. Was soll ich machen? –

+0

können Sie für jedes verwenden, dann löschen Sie das Objekt [[SingletonClass singleton] .arraySubMenuItems removeObject: indexPath]; –

Antwort

2

Sie Objekte wie dieses

rowValue=(int)btn.tag; 
NSMutableArray *arrTemp = [NSMutableArray new]; 
for (int i=0; i<[SingletonClass singleton].arraySubMenuItems.count; i++) 
{ 
    NSIndexPath * Path = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]; 
    int section = (int) Path.section; 
    if (section == rowValue) 
    { 
     [arrTemp addObject:[[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]]; 
    } 
} 
[[SingletonClass singleton].arraySubMenuItems removeObjectsInArray:arrTemp]; 
+0

Sie haben mich gerettet: D –

+0

Froh, dass es geholfen hat :) – Rajat

0
rowValue=(int)btn.tag; 

int countItem = [SingletonClass singleton].arraySubMenuItems.count; 

for (int i=0; i < countItem ; i++) 

{ 

NSIndexPath * Path = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]; 

    int section = (int) Path.section; 

    if (section == rowValue) 
    { 

     NSIndexPath *indexPath = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]; 
      [[SingletonClass singleton].arraySubMenuItems removeObjectAtIndex:i]; 

    } 
} 

speichern Ihre Zählung in verschiedenen variablen und Laufschleife auf, dass entfernen können, weil, wenn Sie von Ihrem Element entfernen indexPath ändert sie Ihren komplette Anzahl.

0

Sie können die Indizes der zu löschenden Elemente in einem Indexset verwenden und die Elemente nach Index entfernen. Das ist was ich tue.

rowValue=(int)btn.tag; 
     NSMutableIndexSet *indicesToRemove = [[NSMutableIndexSet alloc]init]; 
    for (int i=0; i<[SingletonClass singleton].arraySubMenuItems.count; i++) 
    { 
     NSIndexPath * Path = [[SingletonClass singleton].arraySubMenuItems objectAtIndex:i]; 
     int section = (int) Path.section; 
     if (section == rowValue) 
     { 
     [indicesToRemove addIndex:i] 
     } 
    } 
    [[SingletonClass singleton].arraySubMenuItems removeObjectsAtIndexes:indices]; 
Verwandte Themen