CellCache 使用 tornadoFX 在 TableView 中意外呈现
CellCache renders unexpectedly in TableView with tornadoFX
我只是对 TornadoFX
和 Kotlin
中的 cellCache
函数用法感到困惑。渲染总是奇怪和出乎意料,那会发生什么?代码在这里:
data class Person(var name : String, var age : Int, var sex : Boolean)
class MyView: View()
{
val list = listOf<Person>(
Person("Name A", 20, false),
Person("Name B", 22, false),
Person("Name C", 21, true),
Person("Name D", 30, true),
Person("Name E", 35, true)
).observable()
override val root = stackpane {
vbox {
tableview(items = list) {
column("Name", Person::name)
column("Age", Person::age)
column("Sex", Person::sex).cellCache{
checkbox{
isSelected = it
setOnAction {
println("Selection: $isSelected")
}
}
}
}
}
}
}
我希望所有行在 "Sex"
列中呈现 checkbox
,但上面的代码只会显示两个:一个 selected
和另一个 unselected
, cellcache
到底是怎么回事?
我也发现把data class
换成下面的,就可以了:
data class Person(var name : String, var age : Int, var sex : SimpleBooleanProperty)
但是我必须以这种方式对数据 class 属性进行很多更改,我也不知道为什么。
帮助我,提前致谢!
cellCache
为列表中的每个项目创建一个唯一的单元格。如果您的项目没有实现有意义的 equals
和 hashCode
功能,您将 运行 陷入 cellCache
的麻烦,因为同一个单元格将用于多个基础项目。
添加 equals
和 hashCode
确保每个项目都是唯一的实现应该可以解决问题。似乎 Kotlin 数据 class 中的默认实现并没有削减它:)
如果您不能或不会提供 equals
和 hashCode
实现,我建议您改用 cellFormat
,它不依赖于对象相等性来产生唯一性细胞.
很抱歉,指南中没有明确说明这一点,我将更新 TableView 部分,提供更多信息和有关使用 cellCache
的警告。
我只是对 TornadoFX
和 Kotlin
中的 cellCache
函数用法感到困惑。渲染总是奇怪和出乎意料,那会发生什么?代码在这里:
data class Person(var name : String, var age : Int, var sex : Boolean)
class MyView: View()
{
val list = listOf<Person>(
Person("Name A", 20, false),
Person("Name B", 22, false),
Person("Name C", 21, true),
Person("Name D", 30, true),
Person("Name E", 35, true)
).observable()
override val root = stackpane {
vbox {
tableview(items = list) {
column("Name", Person::name)
column("Age", Person::age)
column("Sex", Person::sex).cellCache{
checkbox{
isSelected = it
setOnAction {
println("Selection: $isSelected")
}
}
}
}
}
}
}
我希望所有行在 "Sex"
列中呈现 checkbox
,但上面的代码只会显示两个:一个 selected
和另一个 unselected
, cellcache
到底是怎么回事?
我也发现把data class
换成下面的,就可以了:
data class Person(var name : String, var age : Int, var sex : SimpleBooleanProperty)
但是我必须以这种方式对数据 class 属性进行很多更改,我也不知道为什么。 帮助我,提前致谢!
cellCache
为列表中的每个项目创建一个唯一的单元格。如果您的项目没有实现有意义的 equals
和 hashCode
功能,您将 运行 陷入 cellCache
的麻烦,因为同一个单元格将用于多个基础项目。
添加 equals
和 hashCode
确保每个项目都是唯一的实现应该可以解决问题。似乎 Kotlin 数据 class 中的默认实现并没有削减它:)
如果您不能或不会提供 equals
和 hashCode
实现,我建议您改用 cellFormat
,它不依赖于对象相等性来产生唯一性细胞.
很抱歉,指南中没有明确说明这一点,我将更新 TableView 部分,提供更多信息和有关使用 cellCache
的警告。