Search code examples
iphoneiosobjective-cnsmutablearraynsindexpath

How to add multiple NSIndexPath into NSMutableArray


I am trying to add nsindexpath into an array .. i am able to add only one indexpath .if i try to add another indexpath into array ..the debugger shows only the newest indexpath .

 for (int i = 0 ; i<[notificationArray count]; i++)
        {
            selectedSymptIndexPath = [NSIndexPath indexPathForRow:selectedSymptomIndex inSection:keyIndexNumber];
            selectedSympIPArray = [[NSMutableArray alloc]init];
            [selectedSympIPArray addObject:selectedSymptIndexPath];
        }

even if i try to put [selectedSympIPArray addObject:selectedSymptIndexPath]; outside for loop still its adding only the newest indexpath rather than showing multiple indexpaths


Solution

  • In your code , you alloc every time in iteration. It's totally wrong. Find your mistake.

    You can use this code....

    selectedSympIPArray = [NSMutableArray array];
    for (int i = 0 ; i<[notificationArray count]; i++)
            {
                selectedSymptIndexPath = [NSIndexPath indexPathForRow:selectedSymptomIndex inSection:keyIndexNumber];            
                [selectedSympIPArray addObject:selectedSymptIndexPath];
            }