菜鸟教程小白 发表于 2022-12-13 00:05:34

ios - 在 tableView 问题中重新加载 View 或 reloadData 后选择复选标记


                                            <p><p>我正在尝试创建一个应用程序来选择/取消选择 tableView 中的行,所有数据都是从 coreData 加载的,当我选择一个允许我在 coreData 中更新它的元素时,反之亦然,</p>

<p>一开始一切正常,我从 coreData 加载数据,在 tableView 中显示所有项目,如果需要,在 cellForRowAtindexPath 中检查行</p>

<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

    var cell:UITableViewCell? = nil

    let item = (arrayToDisplay as Array&lt;NSManagedObject&gt;)

    cell = tableView.dequeueReusableCell(withIdentifier: &#34;ShoppingCell&#34;)!

    let lblName:UILabel = cell?.viewWithTag(101) as! UILabel

    let attributedString = NSMutableAttributedString(string: (item.value(forKeyPath: &#34;name&#34;) as! String?)!)

    if( item.value(forKeyPath: &#34;bought&#34;) as! Bool)
    {
      attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
      attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attributedString.length))

      cell?.setSelected(true, animated: true) //set checkMark to active/checked status

    }


    lblName.attributedText = attributedString

    return cell!
}
</code></pre>

<p>在此之前,我已启用如下所示的编辑样式</p>

<pre><code>func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -&gt; UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle(rawValue: 3)!
}
</code></pre>

<p>当我第一次加载 View 时一切正常,当我选中或取消选中更新 CoreData 中的值<strong>并重新加载我的 tableView</strong> 时,我从 <strong> 中的 coreData 和 tableView 重新加载数据viewDidAppear</strong>,所以每当我在场景之间切换时都会使用新数据进行刷新</p>

<p>当我在我的 tableView 中检查一个项目时,我会保存数据、更新我的类数组并 reloadData 以显示新的更改(包括文本删除线)</p>

<p>这就是问题所在,每当我重新加载 tableView 或在选项卡之间切换(在 TabBar 中)并返回时,未正确检查复选标记,我无法检查它们,它们看起来像是已检查并且在不到一秒钟的时间内取消选中,</p>

<p>我已经检查过我的代码中可能存在一些冲突,但一切都是正确的,因为对 coreData 的所有更新操作都已正确完成,</p>

<p>我关注了来自 <a href="https://github.com/MaitrayeeGhosh/TableViewCheckBoxExample" rel="noreferrer noopener nofollow">Maitrayee Ghosh</a> 的示例项目在 ObjC 中编写,您可以通过将 添加到 didSelectRowAtIndexPath 来模拟相同的问题,如下所示</p>

<pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@&#34;user selected %@&#34;,);

;}
</code></pre>

<p>那么,我如何刷新带有复选标记状态的 tableView 内容,并通过更改状态的能力正确检查,例如 Google Keep 复选框注释</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>两件事。</p>

<p>在 <code>cellForRowAt</code> 中,您需要在 <code>if</code> 语句中添加 <code>else</code>。</p>

<pre><code>if( item.value(forKeyPath: &#34;bought&#34;) as! Bool)
{
    attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
    attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attributedString.length))

    cell?.setSelected(true, animated: false)
} else {
    cell?.setSelected(false, animated: false)
}
</code></pre>

<p>单元格会被重复使用,因此您必须始终处理这两种情况。</p>

<p>在您的 <code>didSelectRowAt</code> 方法中,您需要更新数据模型,以便 <code>cellForRowAt</code> 正确呈现单元格。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 tableView 问题中重新加载 View 或 reloadData 后选择复选标记,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42723313/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42723313/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 tableView 问题中重新加载 View 或 reloadData 后选择复选标记