简体   繁体   English

UITableView“cellForRowAt:indexPath”偶尔在核心数据属性上调用“init?(coder aDecoder:NSCoder)”

[英]UITableView “cellForRowAt: indexPath” Occasionally Calling “init?(coder aDecoder: NSCoder)” on Core Data Attribute

I have a core data entity Person with a transformable attribute age of type Age . 我有一个核心数据实体Person ,其变换属性ageAge

final class Person: NSManagedObject {
    @NSManaged public fileprivate(set) var age: Age
}

Age adopts the NSCoding protocol and has two variables value and scale , but only the value is saved: Age采用NSCoding协议,有两个变量valuescale ,但只保存了value

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
    }

}

I display the age of an instance of Person within a UITableViewCell . 我在UITableViewCell显示Person实例的age This instance ( person ) has an age value of 10.0, ie person.age.value = 10.0 , such that when the scale is changed programatically to say scale = 2.0 via a UIStepper , the UITableViewCell displays 20.0 (ie scale * value ). 此实例( person )的年龄值为10.0,即person.age.value = 10.0 ,这样当通过UIStepper以编程方式更改scale = 2.0以表示scale = 2.0UITableViewCell显示20.0 (即scale * value )。

However , I'm finding that if I increment the UIStepper enough times eventually the initialisation of the Age class for the Person is called during the tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell method, which returns an instance of Person at a given IndexPath . 但是 ,我发现如果我增加UIStepper足够的次数,最终会在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell方法中调用PersonAge类的初始化,该方法返回一个实例Person在给定的IndexPath This obviously causes the init?(coder aDecoder: NSCoder) method within the Age class to be called, which resets the value of the scale property to 1. 这显然会导致调用Age类中的init?(coder aDecoder: NSCoder)方法,这会将scale属性的值重置为1。

Why does this happen, please, and is there a way fixed this at all? 请问为什么会发生这种情况,是否有办法解决这个问题? I ideally want the value of the scale property to always remain what it is set to on the UIStepper . 理想情况下,我希望scale属性的值始终保持在UIStepper上设置的UIStepper

Thanks for any help on this matter. 感谢您对此事的任何帮助。

EDIT 编辑

A given person at an indexPath is obtained in the following way: indexPath给定person通过以下方式获得:

private var people: [Person] {
    return Array(database.people).sortedArray(using: Person.defaultSortDescriptors)
}

private func person(at indexPath: IndexPath) -> Person {
    return people[indexPath.item]
}

Your people property is a computed property , which means you get a new people array every time you access it by people[indexPath.item] . 您的people属性是一个计算属性 ,这意味着每次人们访问它时都会获得一个新的人员数组people[indexPath.item] So you are initializing a new Person instance every time you call func person(at:) , which I guess in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell . 所以每次调用func person(at:)时都要初始化一个新的Person实例,我想在tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

Test this by changing the stepper value, and then make the cell disappear from the screen and come back to the same cell. 通过更改步进器值进行测试,然后使单元格从屏幕上消失并返回到同一单元格。 Then the age will have been reset. 然后年龄将被重置。

Just make your people array a stored property like this. 只需让你的人员数组成为这样的存储属性。

private var people: [Person] = Array(database.people).sortedArray(using: Person.defaultSortDescriptors)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从必需的init中的NSCoder获取属性(编码器aDecoder:NSCoder) - Get property from NSCoder in required init(coder aDecoder: NSCoder) 何时在 UIView 或 UIViewController 上调用“必需的 init?(编码器 aDecoder:NSCoder)”? - When is "required init?(coder aDecoder: NSCoder)" called on a UIView or UIViewController? 自定义视图-“必需的init?(编码器aDecoder:NSCoder)”方法崩溃 - Custom view - Crash in “required init?(coder aDecoder: NSCoder)” method 使用编码器aDecoder推送ViewController:NSCoder - Push ViewController with coder aDecoder: NSCoder 当我不使用Storyboard时,在所需的init中是否可以使用fatalError?(编码器aDecoder:NSCoder)? - Is it OK to have fatalError in required init?(coder aDecoder: NSCoder) when I don't use Storyboards? 无法弄清楚编码器 aDecoder: NSCoder - Can't figure out coder aDecoder: NSCoder init编码器究竟是什么? - What exactly is init coder aDecoder? 使用“required init?(coder aDecoder: NSCoder)”进行IBOutlets初始化时,我会遇到任何问题吗? 如果是的话是什么例子? - Will I ever have any kind of problem when using "required init?(coder aDecoder: NSCoder)" for IBOutlets initialization? If yes what's an example? UITableView,cellForRowAt indexPath没有被调用 - UITableView, cellForRowAt indexPath is not being called 如何覆盖 FacebookAuthToken 的 init(coder: NSCoder) - How to override init(coder: NSCoder) for FacebookAuthToken
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM