菜鸟教程小白 发表于 2022-12-12 09:31:49

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


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

<pre><code>-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    BBFilterProductAttribute *productAttribute = self.productAttributes;

    if () {
      if (productAttribute.option) {
            ;
      }
      else {
            ;
      }
    }

    ;
}
</code></pre>

<p>为了解决重用问题,当某些单元格有复选标记时,即使它们没有被选中,我也会在我的 <code>cellForForAtIndexPath:</code> 方法中这样做:</p>

<pre><code>if() {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

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

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

<p>问题是:</p>

<p>如果我在第 2 节中选择了一行,假设它是单选,然后我在第 3 节中选择了一行,也是单选,复选标记从第 2 节移动到第 3 节。</p>

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

<ul>
<li>第 1 部分:(多选):选中所有行</li>
<li>第 2 部分:(单选):选中一行</li>
<li>第 3 部分:(单选):选中一行</li>
</ul>

<p>而不是这个,当我从第 2 部分中选择一行时,它看起来像这样:</p>

<ul>
<li>第 1 部分:(多选):选中所有行</li>
<li>第 2 部分:(单选:已删除先前的选择</li>
<li>第 3 部分:(单选):选中一行</li>
</ul></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>将您的 <code>didSelectRowAtIndexPath:</code> 更改为</p>

<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    BBFilterProductAttribute *productAttribute = self.productAttributes;

    if ()
    {
      if (productAttribute.option)
      {
            ;
      }
      else
      {
         ;
      }
    }
    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 = ;
      NSArray *filteredArray = ;
      if ( &gt; 0)
      {
            //A row from this section selected previously, so remove that row
            ];
      }

      //Add current selected row to selected array
      ;
    }

    ;   
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 分组 UITableView - 允许多选和单选,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23241019/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23241019/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 分组 UITableView - 允许多选和单选