thewarri0r9 thewarri0r9 - 20 days ago 5
iOS Question

Objective-C removing objects from NSArray containing indexpath

I've an array which contains NSIndexPath and I want to remove all objects which have the same IndexPath.Row. My current code has some issues, not all the objects with same Row are removed.
My code is:

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

}
}

Answer

You can remove objects like this

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