如何将多个NSIndexPath添加到NSMutableArray中

rap*_*tor 5 iphone objective-c nsmutablearray nsindexpath ios

我试图将nsindexpath添加到一个数组..我只能添加一个索引路径.如果我尝试将另一个索引路径添加到数组..调试器只显示最新的索引路径.

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

即使我试图将[selectedSympIPArray addObject:selectedSymptIndexPath];外部循环仍然只添加最新的索引路径而不是显示多个索引路径

Sat*_*ran 12

你是每次在循环中分配初始化数组,不要这样做.

试试这个:

selectedSympIPArray = [[NSMutableArray alloc]init];

for (int i = 0 ; i<[notificationArray count]; i++)
{
   selectedSymptIndexPath = [NSIndexPath indexPathForRow:selectedSymptomIndex inSection:keyIndexNumber];
   [selectedSympIPArray addObject:selectedSymptIndexPath];
}
Run Code Online (Sandbox Code Playgroud)


Man*_*ani 8

在您的代码中,您每次都在迭代中进行分配.这是完全错误的.找出你的错误.

你可以使用这个代码......

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