How to add multiple NSIndexPath to NSMutableArray

I am trying to add nsindexpath to an array. I can add only one index path .if I'm trying to add another index path to an array. The debugger shows only the latest index path.

 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];out for loop still, adding only the newest index path and not displaying multiple index paths

+5
source share
2 answers

In your code, you point to iteration every time. This is completely 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];
        }
+8
source

You allocate init every time in the loop, don't do this.

Try the following:

selectedSympIPArray = [[NSMutableArray alloc]init];

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

All Articles