Objective c 如何从NSCollectionView中删除所有项目?

Objective c 如何从NSCollectionView中删除所有项目?,objective-c,cocoa,Objective C,Cocoa,我试图通过从NSMutableArray中删除所有项目来删除用户在NSCollectionView中添加的所有项目。它不起作用了 我只需要通过清除/删除NSCollectionView中的所有项目来刷新/重新加载 NSIndexSet* selectedIndexSet = [_lablesCollectionView selectionIndexes]; NSUInteger index = [selectedIndexSet firstIndex] ; NSLog(@"index is %d

我试图通过从
NSMutableArray
中删除所有项目来删除用户在
NSCollectionView
中添加的所有项目。它不起作用了

我只需要通过清除/删除
NSCollectionView

中的所有项目来刷新/重新加载

NSIndexSet* selectedIndexSet = [_lablesCollectionView selectionIndexes];
NSUInteger index = [selectedIndexSet firstIndex] ;
NSLog(@"index is %d", index);
[lablesArray removeObjectsAtArrangedObjectIndexes:selectedIndexSet];

希望有帮助。

//从NSCollectionView中删除/清除所有项目

 for (int i = 0; i < [[arrayController arrangedObjects] count]; i++) {
        [arrayController removeObjectAtArrangedObjectIndex:i];
        i--;
    }
for(int i=0;i<[[arrayController arrangedObjects]计数];i++){
[阵列控制器移除对象或阵列对象索引:i];
我--;
}

在集合视图上调用reloadData。如果我正在重新加载数据,它将工作并更新。但是,如果我只需要清除NSCollectionView中的所有项目,则它不会得到更新。我已通过removeObjectAtArrangedObjectIndex解决了此问题。感谢回复@Jasper//solution:for(int i=0;i<[arrayController arrangedObjects]计数];i++{[arrayController RemoveObjectataArrangedObjectIndex:i];i--;}请注意,
i
在这段代码中始终为0。您可以将其简化为:
while(arrayController.arrangedObject.count>0){[arrayController removeObjector或arrangeObjectIndex:0];}