gpt4 book ai didi

ios - UICollectionView allowsMultipleSelections 不工作

转载 作者:行者123 更新时间:2023-11-28 22:02:55 25 4
gpt4 key购买 nike

我正在尝试使用 CollectionView 制作一些 photoPicker。

allowsMultipleSelection = YES

使用以下方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

selectedPictures = [NSMutableArray array];

[selectedPictures addObject:[imagesArray objectAtIndex:indexPath.item]];

NSLog(@"Selected list:\n %@", selectedPictures);

NSLog(@"Objects in Array %i", selectedPictures.count);

}

当我选择单元格时,它总是根据它的 indexPath 只向 MutableArray 添加一个对象。可能是什么问题?

最佳答案

为什么不将selectedPictures 保留为成员变量

在你的代码中

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

selectedPictures = [NSMutableArray array]; //keep on creation the new array on each selection

[selectedPictures addObject:[imagesArray objectAtIndex:indexPath.item]]; //adding the selected images means single image

NSLog(@"Selected list:\n %@", selectedPictures);

NSLog(@"Objects in Array %i", selectedPictures.count);

}

试试这个

把他放在viewDidLoad

- (void)viewDidLoad
{
selectedPictures = [[NSMutableArray alloc]init]; //initilise hear
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

// selectedPictures = [NSMutableArray array]; //keep on creation the new array on each selection

[selectedPictures addObject:[imagesArray objectAtIndex:indexPath.item]]; //adding the selected images means single image to already initialised array

NSLog(@"Selected list:\n %@", selectedPictures);

NSLog(@"Objects in Array %i", selectedPictures.count);

}

希望这对你有帮助.. :)

关于ios - UICollectionView allowsMultipleSelections 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com