UITableView - 通过绑定更好地编辑?
链接到问题: JavaFX 2:在 TableCell 中保存编辑
建立可编辑的表格视图似乎需要分配管道 -即捕获每个文本字段的所有事件(获得/失去焦点、从文本字段切换、将编辑从文本字段提交到底层数据模型),并重写表单元格。
建立编辑的默认行为(在单元格中双击)对于我或标准表格控件的用户来说似乎并不熟悉。大多数情况下,我只想单击单元格并开始输入。
有没有完全实现的例子?请添加您的意见或对设计此类生物的评论。
Linked to question: JavaFX 2: Save edit in TableCell
There seems to be allot of plumbing required for to establish an editable tableview - namely trapping all the events for each textField (gained/lost focus, tabbing away from the textField, commiting edits from the textField to the underlying data model), and overriding several methods in the the TableCell.
The default behavior to establish editing - doubleclicking in a cell - doesn't seem familiar to me or users of a standard table control. I just want to click into the cell and start typing, for the most part.
Are there any fully implemented examples out there? Please add yours, or comments for designing such a creature.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们不是响应
TableCell
和TableColumn
级别的多个事件来启动单元格编辑并成功更新单元格的基础数据,而是提供自定义单元格工厂和覆盖单元格中的updateItem()
方法,并将 textField 的 textProperty 直接“绑定”到TableView
数据模型内的属性(在本例中为字符串属性
)。我添加了其他美感,使单元格内的文本字段看起来无缝并响应悬停和聚焦状态。所有的魔法都发生在
updateItem()
方法中。您必须跟踪 textField 及其绑定的内容 - TableView API 会“回收”TableCell 以减少内存消耗:下面是一个包含 4 列的表格的完整示例,所有列都绑定到底层 textField。一旦您在文本字段中输入内容,Observable 列表中的基础数据模型就会更新:
Instead of responding to several events at the
TableCell
andTableColumn
level to initiate editing of a cell, and successfully update the cell's underlying data - instead we provide a custom cell factory and override theupdateItem()
method in the cell and 'bind' the textProperty of the textField directly to the property inside our data model for theTableView
(in this case aStringProperty
). I have added other aesthetics to make the textField inside the cell seem seamless and respond to hover and focused states.All the magic happens in
updateItem()
method. You have to keep track of the textField and what it is bound to - the TableView API 'recycles' TableCells to reduce memory consumption:Here is a full example of a table with 4 columns, all bound to the underlying textField. As soon as you type in the textField, the underlying data model in the Observable list is updated: