菜鸟教程小白 发表于 2022-12-11 21:47:04

ios - 检查字典数组中的值


                                            <p><p>我有 2 个数组。第一个是来自 JSON 提要的字典数组。第二个只是我用来填充 UITableView 的一组值。</p>

<p>对于表格中的每一行,我需要知道我正在写入表格单元格的值是否存在于任何字典的第一个数组中,因此我可以适本地设置附件类型。有没有一种简单的方法可以做到这一点,或者我最好先从每个字典中创建另一个 id 值数组?</p>

<p>我希望这是有道理的,看不到任何其他方式来解释它;-)</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以将 <code>indexesOfObjectsPassingTest:</code> 与在字典中搜索的 block 一起使用,但这不是最理想的:您将一遍又一遍地对每个单元格重新执行相同的搜索,这可能会减慢您的速度应用程序,尤其是当用户快速滚动您的表格时。获得提要后立即使用项目创建 <code>NSSet</code> 并在渲染表格单元格时重新使用它会更好。</p>

<p><strong>示例:</strong></p>

<p>在收到 JSON 提要时构造 <code>NSSet *allIds</code>,如下所示:</p>

<pre><code>allIds = [ init]; // NSSet *allIds is declared
         // in the same class as the array from which you get your table cells
[responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    ];
}];
</code></pre>

<p>当您需要决定是否显示附件 View 时,请执行以下操作:</p>

<pre><code>if () {
    // Add accessory view
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 检查字典数组中的值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/9618316/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/9618316/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 检查字典数组中的值