【发布时间】:2015-12-29 08:39:27
【问题描述】:
在previous question 中,我询问如何在 GUI 中显示Dictionary 的内容。我从这个想法开始构建一个外观和感觉稍微好一点的 GUI。它主要由在列和行中粘合在一起的RectangleMorphs 组成(参见我上一个问题中接受的答案)。
现在的问题是我希望在我的字典中添加/删除/编辑元素时更新我的表格。我设法编写了一些Morph,其中包含CellMorphs 的列,它们继承自RectangleMorph,并将model 和message 作为实例变量,并带有以下更新消息:
update
" update the contents of this cell "
| value |
self removeAllMorphs.
(model = nil or: message = nil)
ifTrue: [ value := '' ]
ifFalse: [ value := model perform: message ].
self addMorph: value asMorph.
可以看出,CellMorph 是Morph 的容器,其中包含单元格的实际内容。这非常适合显示字典的大小,例如:
d := Dictionary new.
d at: 'foo' put: 100.
d at: 'bar' put: 200.
cell := CellMorph new
model: d;
message: #size;
color: Color white.
cell openInWorld.
d at: 'boo' put: 300. " cell will be updated "
但我似乎没有为字典的内容找到类似的东西,因为我找不到通过消息访问单个键或值的方法。我能想到的唯一解决方案是每次都使用新单元格创建新列,但这太昂贵了,我无法想象这是个好主意...
因此我的问题:
有没有办法更新我的Morph 显示字典而不创建数十亿我的CellMorphs 或者我应该忘记我的想法而是使用CellMorphs 的行例如以便对字典中的条目进行分组?
为了完整性:CellMorph 中的 model: 消息看起来像:
model: newModel
"change the model behind this cell"
model ifNotNil: [ model removeDependent: self ].
newModel ifNotNil: [newModel addDependent: self].
model := newModel.
self update.
update: aParameter 只是调用update.,我还在Dictionary 的所有消息中添加了self changed.,我希望通知接口(@987654343@、removeKey: 等)。
【问题讨论】:
标签: user-interface observer-pattern smalltalk squeak