• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:03:35 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

**

I have horizontal CollectionView in which selected cell have orange gradient color and all other deselected cells gray color i m using only didselect delegate method but i am getting problem of multiple cells selected and i have problem with same concept in table view while scrolling and i have searched a lot but i have not get proper answer of cell reusability

**

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)indexPath
{


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



    if (self.selectedIndexPath != nil && indexPath == self.selectedIndexPath)
    {

        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.frame = cell.mainView.bounds;
        gradient.startPoint = CGPointZero;

        gradient.endPoint = CGPointMake(1, 1);
        gradient.colors = [NSArray arrayWithObjectsid)[[UIColor colorWithRed:238.0/255.0 green:42.0/255.0 blue:123/255.0 alpha:1.0] CGColor],(id)[[UIColor colorWithRed:241.0/255.0 green:90.0/255.0 blue:41.0/255.0 alpha:1.0] CGColor], nil];
        [gradient setMasksToBounds:NO];
        cell.mainView.backgroundColor = [UIColor clearColor];
        [cell.mainView.layer insertSublayer:gradient atIndex:0];

        [indexPaths addObject:self.selectedIndexPath];

    }
     else
     {
         [cell.mainView setBackgroundColor:[self colorWithHexString"383F4A"]];
      }

    return cell;
}

-(void)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath
{


    indexPaths = [NSMutableArray arrayWithObjects:indexPath, nil];

    if (self.selectedIndexPath)
    {
        // if we had a previously selected cell

        if ([indexPath compare:self.selectedIndexPath] == NSOrderedSame)
        {
            // if it's the same as the one we just tapped on, then we're unselecting it

            NSLog(@"Selected");

        }
        else
        {
            // if it's different, then add that old one to our list of cells to reload, and
            // save the currently selected indexPath

            [indexPaths addObject:self.selectedIndexPath];
            self.selectedIndexPath = indexPath;
        }
    }
    else
    {
        // else, we didn't have previously selected cell, so we only need to save this indexPath for future reference

        self.selectedIndexPath = indexPath;

    }



    dispatch_async(dispatch_get_main_queue(), ^{
         [collectionView reloadItemsAtIndexPaths:indexPaths];
    });


}



Best Answer-推荐答案


像下面这样全局获取一个全局 selectedIndexPath

NSIndexPath *selectedIndexPath = [[NSIndexPath alloc] indexPathForRow:0 inSection:0];

在委托(delegate)方法中编写下面的代码

- (UICollectionViewCell *)collectionViewUICollectionView *)collectionView cellForItemAtIndexPathNSIndexPath *)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)collectionViewUICollectionView *)collectionView didSelectItemAtIndexPathNSIndexPath *)indexPath
{

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


 }

关于ios - 如何在 CollectionView 中选择单个单元格并取消选择所有其他单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592767/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap