在 tableView 问题中重新加载视图或 reloadData 后选择复选标记
Selecting checkmarks after reloading view or reloadData in tableView issue
我正在尝试为 tableView 中的 select/deselect 行创建一个应用程序,所有数据都是从 coreData 加载的,当我 select 一个允许我在 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)!
}
当我第一次加载视图时一切正常,当我选中或取消选中我更新 CoreData 中的值 并重新加载我的 tableView,我从 coreData 重新加载数据并且viewDidAppear 中的 tableView,所以每当我在场景之间切换时,都会使用新数据进行刷新
当我检查 tableView 中的项目时,我保存数据,更新我的 class 数组,并重新加载数据以显示新的更改(包括文本删除线)
这就是问题所在,无论何时我重新加载 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 保留复选框注释
两件事。
在 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
正确呈现单元格。
我正在尝试为 tableView 中的 select/deselect 行创建一个应用程序,所有数据都是从 coreData 加载的,当我 select 一个允许我在 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)!
}
当我第一次加载视图时一切正常,当我选中或取消选中我更新 CoreData 中的值 并重新加载我的 tableView,我从 coreData 重新加载数据并且viewDidAppear 中的 tableView,所以每当我在场景之间切换时,都会使用新数据进行刷新
当我检查 tableView 中的项目时,我保存数据,更新我的 class 数组,并重新加载数据以显示新的更改(包括文本删除线)
这就是问题所在,无论何时我重新加载 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 保留复选框注释
两件事。
在 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
正确呈现单元格。