Swift TLV 编辑器:更新 NSOutlineView 中的父项以添加/删除项

Swift TLV Editor: Updating parent items in an NSOutlineView for adding/deleing items

提问人:Fritz 提问时间:3/12/2023 最后编辑:Fritz 更新时间:3/12/2023 访问量:29

问:

我想做的是构建一个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
             }
....
Swift NSoutlineView TLV

评论

0赞 Willeke 3/12/2023
问题是关于更新数据还是关于?请在问题中发布一个最小的可重复示例NSOutlineView
0赞 Willeke 3/13/2023
之后的数据是否正确?viewModel.remove(item: selectedItem, from: construction)
0赞 Fritz 3/24/2023
实际上添加/删除并不像我认为的那样有效。如果以线性方式添加项目,则添加有效。稍后添加/插入构造时会出现问题。肯定有一个问题,因为我尝试添加而不是插入。删除效果更糟。很容易产生不同步的东西......

答: 暂无答案