我有一个包含NSIndexPath的数组,我想删除所有具有相同IndexPath.Row的对象。我当前的代码存在一些问题,并非所有具有相同Row的对象都被删除。
我的代码是:

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

    }
}

最佳答案

您可以删除这样的对象

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

关于ios - Objective-C从包含索引路径的NSArray中删除对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40655668/

10-11 14:36