草庐IT

ios - 重新加载后无法取消选择 UICollectionView 单元格

coder 2023-09-24 原文

我在使用 UIcollectionView 时遇到了一系列问题,我认为解决方法是当前导致问题的原因。

好的,所以我有一个 collectionview,它从一个更新非常频繁的类中加载对象,可能每隔几秒更新一次。

我遇到的第一个问题;是当我选择一个单元格并将其突出显示时, Collection View 下方的另一个单元格也突出显示。我的解决方案是创建一个选定单元格数组,然后在新数组中运行一个循环来决定要突出显示的内容。

现在,问题出现在重新加载 collectionview 时。这些单元格继续保持突出显示并显示为选中状态,但它们没有记录触摸,因此我无法取消选择它。

    - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:        (NSInteger)section
{
    return [self.deviceList count];
}

- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
{
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DeviceCollectionViewCell *cell = [self.deviceCollectionView dequeueReusableCellWithReuseIdentifier:@"DeviceCell" forIndexPath:indexPath];

    if([self.deviceList count] > indexPath.row)
    {
        cell.device = [self.deviceList objectAtIndex:indexPath.row];
    }

    if ([[self.deviceCollectionView indexPathsForSelectedItems] containsObject:indexPath]) {
        NSLog(@"does it ever?");

       // cell.backgroundColor = [UIColor blueColor];
    }


    for (TDDeviceParser *device in self.selectedDevices)
    {
        if ([cell.device.deviceTextRecord.serialNumber isEqualToString:device.deviceTextRecord.serialNumber])
        {
            [cell setSelected:YES];
            break;
        }
        else
        {
            [cell setSelected:NO];
        }
    }
    return cell;
}

- (UICollectionReusableView *)collectionView: (UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        TitleHeaderForDeviceCollectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        reusableview = headerView;
    }

    else if (kind == UICollectionElementKindSectionFooter) {
        LogoFooterForDeviceCollectionView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
        reusableview = footerView;
    }
    return reusableview;
}

#pragma mark - UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //Iphone Five and Older Ipads: Max2
    int maxCount = 2;
    //ipad 4
    if ([[UIDeviceHardwareVersion platform] isEqualToString:@"iPad3,4"] || [[UIDeviceHardwareVersion platform] isEqualToString:@"iPad3,5"] || [[UIDeviceHardwareVersion platform] isEqualToString:@"iPad3,6"])
    {
        maxCount = 4;
    }
    //Ipod5th,Iphone4 & Iphone4s; Don't select, just play.
    else if ([[UIDeviceHardwareVersion platform] isEqualToString:@"iPod5,1"] ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone4,1"]  ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone3,1"]  ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone3,2"]  ||  [[UIDeviceHardwareVersion platform] isEqualToString:@"iPhone3,3"])
    {
        maxCount = 0;
        //Just play.
        [self.selectedDevices removeAllObjects];
        [self.selectedDevices addObject:[self.deviceList objectAtIndex:indexPath.row]];
        [[TDDeviceManager sharedInstance] removeObserver:self];
        [self performSegueWithIdentifier:@"VideoPlayer" sender:self];
    }

    if (self.selectedDevices.count < maxCount)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

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



    [self.selectedDevices addObject:[self.deviceList objectAtIndex:indexPath.row]];
    [self updatePlayerImages];
    NSLog(@"device Count %d",self.selectedDevices.count);



}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"Should I Deselect?");
    return YES;
}



- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s",__PRETTY_FUNCTION__);
    //What device do we remove?
    for (TDDeviceParser *device in self.selectedDevices)
    {
        if ([device.deviceTextRecord.serialNumber isEqualToString:((TDDeviceParser*)[self.deviceList objectAtIndex:indexPath.row]).deviceTextRecord.serialNumber])
        {
             NSLog(@"%s Removed %@",__PRETTY_FUNCTION__, device.name);
            [self.selectedDevices removeObject:device];
            break;
        }
    }
     NSLog(@"%s Update %d",__PRETTY_FUNCTION__, self.selectedDevices.count);
    [self updatePlayerImages];
}

最佳答案

我认为问题在于单元格已被选中,但 CollectionView 并不知道单元格已被选中。我添加了(第二行)来解决问题。

        [cell setSelected:YES];
        [self.deviceCollectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];

关于ios - 重新加载后无法取消选择 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731551/

有关ios - 重新加载后无法取消选择 UICollectionView 单元格的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 无法使用 Rails 3.2 创建插件? - 2

    我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby​​1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在

  3. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  4. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  5. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  6. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  7. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  8. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  9. ruby - 无法覆盖 irb 中的 to_s - 2

    我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)

  10. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

随机推荐