2016-11-17 4 views
0

NSIndexPathを含む配列があり、同じIndexPath.Rowを持つすべてのオブジェクトを削除します。私の現在のコードにはいくつかの問題がありますが、同じRowを持つすべてのオブジェクトが削除されるわけではありません。 私のコードは次のとおりです。Objective-C indexpathを含むNSArrayからオブジェクトを削除する

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

あなたは配列を変更しながら(特にアイテムを削除して)、反復処理しています。 – Larme

+0

ええ、私は知っています。私は何をすべきか? –

+0

それぞれを使用して、オブジェクト[[SingletonClass singleton] .arraySubMenuItems removeObject:indexPath]を削除できます。 –

答えて

2

あなたはそれがあなたを変えるindexpathあなたからあなたの項目を削除するときので、その上の別の変数と実行ループでこの

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

あなたは私を救った:D –

+0

それは助けて嬉しい:) – 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]; 

    } 
} 

店あなたのカウントのようなオブジェクトを削除することができます総数。

0

インデックスセットで削除するアイテムのインデックスを取得し、インデックスでアイテムを削除できます。 これは私が行うことです。

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]; 
関連する問題