提问人:Fritz 提问时间:3/12/2023 最后编辑:Fritz 更新时间:3/12/2023 访问量:29
Swift TLV 编辑器:更新 NSOutlineView 中的父项以添加/删除项
Swift TLV Editor: Updating parent items in an NSOutlineView for adding/deleing items
问:
我想做的是构建一个TLV编辑器。TLV 结构由构造和正常项目组成。当我将项目添加到构造中时,我需要更新构造的长度以及上述所有父构造的长度。
我试图通过添加到构造模型来做到这一点。var parent: Construction?
添加它看起来不错,但是删除它时会不同步。有人知道出了什么问题吗?还是有更好的解决方案?
下面是一个简短的演示: https://github.com/frcocoatst/test1
型号:.swift:
class Item: Equatable, CustomStringConvertible {
var id: Int?
var tag: String = "TAG"
var length: Int = 1
var value: String = "VALUE"
...
class Construction: Equatable {
var id: Int?
var clength: Int = 0
var parent: Construction?
var items = [Any]()
...
ViewController.swift添加一个项目:
@IBAction func addItemAction(_ sender: Any) {
print("addItemAction")
// Make sure that there is a target construction to add a new item to.
guard let construction = getConstructionForSelectedItem() else { return }
// Create and get the instance of the new item.
let newItem = viewModel.addItem(
tag: itemTagOutlet.stringValue,
length: itemValueOutlet.stringValue.count/2,
value: itemValueOutlet.stringValue,
comment: "ItemComment",
construction: construction)
print("update construction length")
construction.clength += itemValueOutlet.stringValue.count/2
...
// for all parents add length
var parentConstruction = construction.parent
while let pConstruction = parentConstruction
{
print(pConstruction)
print(">>>update parent construction length")
parentConstruction?.clength += itemValueOutlet.stringValue.count/2
parentConstruction = pConstruction.parent
}
print("reload")
// Reload the outline view and expand the construction.
outlineView.reloadData()
outlineView.expandItem(construction)
ViewController.swift删除一个项目:
@IBAction func deleteItemAction(_ sender: Any) {
print("deleteItemAction")
let selectedRow = outlineView.selectedRow
var result = false
if let selectedItem = outlineView.item(atRow: selectedRow) as? Item {
guard let construction = getConstructionForSelectedItem() else { return }
let ctr = selectedItem.length
construction.clength -= ctr
viewModel.remove(item: selectedItem, from: construction)
//
print("remove item update construction length")
...
// for all parents subtract length
var parentConstruction = construction.parent
while let pConstruction = parentConstruction
{
print(pConstruction)
print(">>>update parent construction length")
parentConstruction?.clength -= ctr
parentConstruction = pConstruction.parent
}
....
答: 暂无答案
评论
NSOutlineView
viewModel.remove(item: selectedItem, from: construction)