5

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

raptor
  • 291
  • 1
  • 9
  • 17

2 Answers2

12

You are alloc init'ing the array every time in the loop, dont do that.

Try this:

selectedSympIPArray = [[NSMutableArray alloc]init];

for (int i = 0 ; i<[notificationArray count]; i++)
{
   selectedSymptIndexPath = [NSIndexPath indexPathForRow:selectedSymptomIndex inSection:keyIndexNumber];
   [selectedSympIPArray addObject:selectedSymptIndexPath];
}
Satheesh
  • 10,998
  • 6
  • 50
  • 93
8

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];
        }
Mani
  • 17,549
  • 13
  • 79
  • 100