如何将多个NSIndexPath添加到NSMutableArray中

5
我试图将nsindexpath添加到数组中,但我只能添加一个indexpath。如果我尝试将另一个indexpath添加到数组中,调试器只会显示最新的indexpath。
 for (int i = 0 ; i<[notificationArray count]; i++)
        {
            selectedSymptIndexPath = [NSIndexPath indexPathForRow:selectedSymptomIndex inSection:keyIndexNumber];
            selectedSympIPArray = [[NSMutableArray alloc]init];
            [selectedSympIPArray addObject:selectedSymptIndexPath];
        }

即使我试图将 [selectedSympIPArray addObject:selectedSymptIndexPath]; 放在 for 循环之外,它仍然只添加最新的 indexPath 而不是显示多个 indexPath。
2个回答

12

你每次都在循环中使用alloc init分配数组,不要这样做。

尝试这样:

selectedSympIPArray = [[NSMutableArray alloc]init];

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

但是当我滚动UITableView一段时间后,它会变慢。 - raptor

8

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

你可以使用以下代码...

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接