如何使用Javascript for Automation加载NSTableView

时间:2014-07-24 14:27:31

标签: javascript automation nstableview osx-yosemite javascript-automation

我想我正试图在OS X Yosemite上试图通过ScriptEditor使用JavaScript来填充NSTableView。我可以显示表格并正确设置行数,但实际数据未在表格中加载。

我确实在控制台中看到了这一点:7/24/14 7:47:27.784 AM脚本编辑器[11708]: * 非法的NSTableView数据源()。必须实现numberOfRowsInTableView:和tableView:objectValueForTableColumn:row:

但是我认为我已经实施了正确的方法。也许我错过了一些简单的东西,或者Javascript-Cocoa绑定中可能存在错误。我看到我的numberOfRowsInTableView的日志语句,但从不用于objectValueForTableColumn。

有什么想法吗?

function makeTable() {
tableContainer = $.NSScrollView.alloc.initWithFrame($.NSMakeRect(10, 150, 380, 200));
tableView = $.NSTableView.alloc.initWithFrame($.NSMakeRect(0, 0, 364, 200));

column1 = $.NSTableColumn.alloc.initWithIdentifier("Col1");
column1.setWidth(252);
column1.headerCell.setStringValue("First Name");

column2 = $.NSTableColumn.alloc.initWithIdentifier("Col2"); 
column2.setWidth(198);
column2.headerCell.setStringValue("Last Name");

tableView.addTableColumn(column1);
tableView.addTableColumn(column2);

if($.MyDataSource==undefined) {
    ObjC.registerSubclass({
        name: "MyDataSource",
        protocols: ["NSTableViewDataSource"],
        methods: {
            "numberOfRowsInTableView:": {
                types: ["NSInteger"],
                implementation: function() {
                    console.log("numberOfRowsInTableView called");
                    return 3;
                }
            },
            "objectValueForTableColumn": {
                types: ["id", ["NSTableColumn", "NSInteger"]],                  
                implementation: function(col, row) {
                    console.log("objectValueForTableColumn called")
                    return "Scott"
                }
            }                       
        }
    }); 
}   

tableDataSource = $.MyDataSource.alloc.init;
tableView.setDataSource(tableDataSource);
tableContainer.setDocumentView(tableView);

window.contentView.addSubview(tableContainer);
}

1 个答案:

答案 0 :(得分:0)

看起来我的objectValueForTableColumn行的方法名称错误:

ObjC.registerSubclass({
    name: "MyDataSource",
    protocols: ["NSTableViewDataSource"],
    methods: {
        "numberOfRowsInTableView:": {
            types: ["long", ["id"]],
            implementation: function(id) {
                console.log("numberOfRowsInTableView called", id);
                return 3;
            }
        },
        "tableView:objectValueForTableColumn:row:": {
            types: ["id", ["id", "id", "long"]],                    
            implementation: function(table, column, row) {
                console.log("objectValueForTableColumn called", table, column);
                return "Scott"
            }
        }                       
    }
});