【问题标题】:CPTreeController (Cappuccino)CPTreeController(卡布奇诺)
【发布时间】:2011-11-30 08:03:11
【问题描述】:

在卡布奇诺中构建绑定兼容的大纲视图数据源的最佳方法是什么?即一种 CPTreeController

我的源当前是一个 JSON 对象(包含对象和数组),我想将它显示到大纲视图中,并且能够更改其参数/获得更改通知。 (一旦加载到 CPTreeController 中,我不需要将其序列化回 JSON,我将直接使用数据源)

然后:

  • 是否有隐藏的 CPTreeController 或类似的库可供使用?
  • 如果我重写自己的数据源,我应该从头开始编写,还是可以轻松混合 CPDictionaries 和 CPArrays 来完成这项任务? (请记住,它应该符合绑定)

【问题讨论】:

    标签: cappuccino


    【解决方案1】:

    通过来源搜索表明没有隐藏的 CPTreeController,因此您可以编写自己的 CPTreeController 实现并将其贡献给社区,也可以为特定模型实现数据源协议,如下所示:

    - (int)outlineView:(CPOutlineView)theOutlineView numberOfChildrenOfItem:(id)theItem
    {
        if (theItem == nil)
            theItem = rootNode;
    
        return [[theItem childNodes] count];
    }
    
    - (id)outlineView:(CPOutlineView)theOutlineView child:(int)theIndex ofItem:(id)theItem
    {
        if (theItem == nil)
            theItem = rootNode;
    
        return [[theItem childNodes] objectAtIndex:theIndex];
    }
    
    - (BOOL)outlineView:(CPOutlineView)theOutlineView isItemExpandable:(id)theItem
    {
        if (theItem == nil)
            theItem = rootNode;
    
        return [[theItem childNodes] count] > 0;
    }
    
    - (id)outlineView:(CPOutlineView)anOutlineView objectValueForTableColumn:(CPTableColumn)theColumn byItem:(id)theItem
    {
        return [[theItem representedObject] valueForKey:"name"];
    }
    

    【讨论】:

    • 我想知道CPTree 是否也没有什么关系。那么哪个对象将负责键绑定?我的数据源还是我要添加的对象?
    • CPTreeNode 有很大帮助,因为您不必重新实现树数据结构。在上面的示例中,rootNode 和 theItem 的意思是 CPTreeNodes。由于没有显式绑定,由您决定哪个对象将负责键绑定,但对我而言,CPTreeNode 似乎也将树结构与实际数据分开,因此最好绑定到添加的对象,例如 @987654328 @.
    猜你喜欢
    • 2010-11-03
    • 2011-05-16
    • 1970-01-01
    • 2011-05-07
    • 2011-04-01
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多