gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:25:15 27 4
gpt4 key购买 nike

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

一开始一切正常,我从 coreData 加载数据,在 tableView 中显示所有项目,并根据需要在 cellForRowAtindexPath 中检查行

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell:UITableViewCell? = nil

let item = (arrayToDisplay[indexPath.section] as Array<NSManagedObject>)[indexPath.row]

cell = tableView.dequeueReusableCell(withIdentifier: "ShoppingCell")!

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

let attributedString = NSMutableAttributedString(string: (item.value(forKeyPath: "name") as! String?)!)

if( item.value(forKeyPath: "bought") 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!
}

在此之前,我启用了如下所示的 editingStyle

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle(rawValue: 3)!
}

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

当我检查我的 tableView 中的项目时,我保存数据,更新我的类数组,并重新加载数据以显示新的更改(包括文本删除线)

这就是问题所在,无论何时我重新加载 tableView,或在选项卡之间切换(在 TabBar 中),然后返回,复选标记都没有正确检查,我无法检查它们,它们看起来像被检查过一样在不到一秒的时间内取消选中,

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

我关注了来自 Maitrayee Ghosh 的示例项目在 ObjC 中编写,您可以通过将 [tableView reloadData] 添加到 didSelectRowAtIndexPath 来模拟相同的问题,如下所示

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);

[tableView reloadData];}

那么,我如何刷新 tableView 内容并正确检查复选标记状态并能够更改状态,例如 Google Keep 复选框注释

最佳答案

两件事。

cellForRowAt 中,您的 if 语句需要一个 else

if( item.value(forKeyPath: "bought") 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)
}

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

在您的 didSelectRowAt 方法中,您需要更新数据模型,以便 cellForRowAt 正确呈现单元格。

关于ios - 在 tableView 问题中重新加载 View 或 reloadData 后选择复选标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42723313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com