final class Person: NSManagedobject { @NSManaged public fileprivate(set) var age: Age}
Age采用NSCoding协议,有两个变量值和比例,但只保存了值:
class Age: NSObject,NSCoding { @objc public var value: Double public var scale = 1.0 overrIDe public var description: String { return "\(scale * value)" } func encode(with aCoder: NSCoder) { aCoder.encode(value,forKey: #keyPath(value)) } public convenIEnce required init?(coder aDecoder: NSCoder) { self.init(value: aDecoder.decodeDouble(forKey: #keyPath(value))) } init(value: Double) { self.value = value }}
我在UItableVIEwCell中显示Person实例的年龄.该实例(人)具有10.0的年龄值,即person.age.value = 10.0,使得当通过UIStepper以编程方式改变比例以表示比例= 2.0时,UItableVIEwCell显示20.0(即,比例*值).
但是,我发现如果我增加UIStepper足够的次数,最终在tableVIEw期间调用Person的Age类的初始化(_ tableVIEw:UItableVIEw,cellForRowAt indexPath:IndexPath) – > UItableVIEwCell方法,它返回给定IndexPath处的Person实例.这显然会导致调用Age类中的init?(coder aDecoder:NSCoder)方法,这会将scale属性的值重置为1.
请问为什么会发生这种情况,是否有办法解决这个问题?理想情况下,我希望scale属性的值始终保持在UIStepper上设置的值.
感谢您对此事的任何帮助.
编辑
indexPath中的给定人员通过以下方式获得:
private var people: [Person] { return Array(database.people).sortedArray(using: Person.defaultSortDescriptors)}private func person(at indexPath: IndexPath) -> Person { return people[indexPath.item]}解决方法 您的人员属性为 computed property,这意味着每次人们访问它时都会获得一个新的人员阵列[indexPath.item].所以每次调用func person(at :)时都要初始化一个新的Person实例,我想在tableVIEw(_ tableVIEw:UItableVIEw,cellForRowAt indexPath:IndexPath) – > UItableVIEwCell中.
通过更改步进器值进行测试,然后使单元格从屏幕上消失并返回到同一单元格.然后年龄将被重置.
只需让你的人员数组成为这样的存储属性.
private var people: [Person] = Array(database.people).sortedArray(using: Person.defaultSortDescriptors)总结
以上是内存溢出为你收集整理的ios – UITableView“cellForRowAt:indexPath”偶尔在核心数据属性上调用“init?(coder aDecoder:NSCoder)”全部内容,希望文章能够帮你解决ios – UITableView“cellForRowAt:indexPath”偶尔在核心数据属性上调用“init?(coder aDecoder:NSCoder)”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)