Ios 如何在CollectionView中选择单个单元格并取消选择所有其他单元格

Ios 如何在CollectionView中选择单个单元格并取消选择所有其他单元格,ios,objective-c,uicollectionview,uicollectionviewcell,Ios,Objective C,Uicollectionview,Uicollectionviewcell,** 我有水平收集视图,其中所选单元格为橙色 渐变色和所有其他取消选择的单元格为灰色 仅使用didselect委托方法,但我遇到了以下问题 选择了多个单元格,我对表中的相同概念有问题 滚动时查看,我搜索了很多,但没有找到 细胞可重用性的正确答案 ** 采用如下所示的全局selectedIndexPath全局 NSIndexPath *selectedIndexPath = [[NSIndexPath alloc] indexPathForRow:0 inSection:0]; 在委托方法中写入

**

我有水平收集视图,其中所选单元格为橙色 渐变色和所有其他取消选择的单元格为灰色 仅使用didselect委托方法,但我遇到了以下问题 选择了多个单元格,我对表中的相同概念有问题 滚动时查看,我搜索了很多,但没有找到 细胞可重用性的正确答案

**


采用如下所示的全局selectedIndexPath全局

NSIndexPath *selectedIndexPath = [[NSIndexPath alloc] indexPathForRow:0 inSection:0];
在委托方法中写入以下代码

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{


DateTimeCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"DateTimeCellID" forIndexPath:indexPath];

if self.selectedIndexPath == indexPath
{
  // do what you want to do with your selected cell
}
else
{
    // do what you want to do with your deselected cell
}


return cell;
}

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

self.selectedIndexPath = indexPath
[self.collectionView reloadData];


 }

您需要为此重新加载所有行。现在一次单击即可选择多个单元格。重新加载后,请管理所需的选定单元格。您可以给出一些示例吗?请直截了当地看!跟踪选定模式下所需单元格的索引,否则将取消选择。在reload.in.h文件(其中有collectionview的出口)之后使用此逻辑
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{


DateTimeCell * cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:@"DateTimeCellID" forIndexPath:indexPath];

if self.selectedIndexPath == indexPath
{
  // do what you want to do with your selected cell
}
else
{
    // do what you want to do with your deselected cell
}


return cell;
}

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

self.selectedIndexPath = indexPath
[self.collectionView reloadData];


 }