草庐IT

ios - 分组的 UITableView - 允许多选和单选

coder 2024-01-19 原文

我有一个 UITableView,它从 NSArray 获取数据。该数组包含模型对象。我已将 UITableView 分成几个部分。现在我试图让一些部分可以多选,而其他部分只能单选。我的模型对象有一个属性,我用它来确定我是需要多选还是单选。我快到了——我已经设法让多选和单选在正确的部分工作。这是代码:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    BBFilterProductAttribute *productAttribute = self.productAttributes[indexPath.section];

    if ([productAttribute.filterType isEqualToString:@"MULTI_SELECT_LIST"]) {
        if (productAttribute.option[indexPath.row]) {
            [self.selectedRows addObject:indexPath];
        }
        else {
            [self.selectedRows removeObject:indexPath];
        }
    }

    [self.tableView reloadData];
}

为了解决重用问题,当一些单元格有复选标记时,即使它们没有被选中,我在我的 cellForAtIndexPath: 方法中这样做:

if([self.selectedRows containsObject:indexPath]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

//For single selection 
else if (self.selectedIndexPath.row == indexPath.row &&
         self.selectedIndexPath.section == indexPath.section) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

我对每个部分的选择都正常工作。每个部分允许多选或单选 - 取决于 didSelectRowAtIndexPath: 方法中的 if 语句。

问题是:

如果我在第 2 部分选择一行,假设它是单选,然后我在第 3 部分选择一行,也是单选,复选标记从第 2 部分移动到第 3 部分。

我需要第 2 部分和第 3 部分保持单一选择 - 但允许两者同时选择一行。所以它应该是这样的:

  • 第 1 部分:(多选):选中所有行
  • 第 2 部分:(单选):选中一行
  • 第 3 部分:(单选):选中一行

取而代之的是,当我从第 2 部分选择一行时,它看起来像这样:

  • 第 1 部分:(多选):选中所有行
  • 第 2 部分:(单选:移除之前的选择
  • 第 3 部分:(单选):选中一行

最佳答案

将您的 didSelectRowAtIndexPath: 更改为

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    BBFilterProductAttribute *productAttribute = self.productAttributes[indexPath.section];

    if ([productAttribute.filterType isEqualToString:@"MULTI_SELECT_LIST"])
    {
        if (productAttribute.option[indexPath.row])
        {
            [self.selectedRows addObject:indexPath];
        }
        else
        {
           [self.selectedRows removeObject:indexPath];
        }
    }
    else
    {
        //Section is SINGLE_SELECTION
        //Checking self.selectedRows have element with this section, i.e. any row from this section already selected or not
        NSPredicate *predicate = [NSPredicate predicatewithFormat:@"section = %d", indexPath.section];
        NSArray *filteredArray = [self.selectedRows filteredArrayUsingPredicate:predicate];
        if ([filteredArray count] > 0)
        {
            //A row from this section selected previously, so remove that row
            [self.selectedRows removeObject:[filteredArray objectAtIndex:0]];
        }

        //Add current selected row to selected array
        [self.selectedRows addObject:indexPath];
    }

    [self.tableView reloadData];    
}

关于ios - 分组的 UITableView - 允许多选和单选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23241019/

有关ios - 分组的 UITableView - 允许多选和单选的更多相关文章

  1. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  2. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  3. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  4. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  5. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  6. ruby - 在 Ruby 中创建按公共(public)键值分组的新哈希 - 2

    假设我有一个在Ruby中看起来像这样的哈希:{:ie0=>"Hi",:ex0=>"Hey",:eg0=>"Howdy",:ie1=>"Hello",:ex1=>"Greetings",:eg1=>"Goodday"}有什么好的方法可以将它变成如下内容:{"0"=>{"ie"=>"Hi","ex"=>"Hey","eg"=>"Howdy"},"1"=>{"ie"=>"Hello","ex"=>"Greetings","eg"=>"Goodday"}} 最佳答案 您要求一个好的方法来做到这一点,所以答案是:一种您或同事可以在六个月后理解

  7. ruby-on-rails - Rails 单选按钮 - 模型中多列的一种选择 - 2

    我希望用户从一个模型的三个选项中选择一个。即我有一个模型视频,可以被评为正面/负面/未知目前我有三列bool值(pos/neg/unknown)。这是处理这种情况的最佳方式吗?为此,表单应该是什么样的?目前我有类似的东西但显然它允许多项选择,而我试图将它限制为只有一个..怎么办? 最佳答案 如果要使用字符串列,让我们说rating。然后在你的表单中:#...#...它只允许一个选择编辑完全相同但使用radio_button_tag: 关于ruby-on-rails-Rails单选按钮-模

  8. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  9. ruby - 允许主机名包含下划线的 URI.parse 的替代方法 - 2

    我正在使用DMOZ的listofurltopics,其中包含一些具有包含下划线的主机名的url。例如:608609TheOuterHeaven610InformationandimagegalleryofMcFarlane'sactionfiguresforTrigun,Akira,TenchiMuyoandotherJapaneseSci-Fianimations.611Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures612虽然此url可以在网络浏览器中使用(或者至少在我的浏览器中可以使用:

  10. arrays - 如何在下面的示例中将两个值数组分组为 n 个值数组? - 2

    我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby​​库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_

随机推荐