MochaJSDelegate
MochaJSDelegate copied to clipboard
Can't set row count in NSTableViewDataSource using MochaJSDelegate
Hi, I'm trying to use NSTableView in sketch plugin, but can't to set row count in NSTableViewDataSource using MochaJSDelegate. How to use it in NSTableViewDataSource?
Thanks
Can you share the code you're using?
I use this code for create NSTableView in cocoascript:
var items = ["icon", "icon/a", "icon/b", "icon/c", "icon/d", "icon/e", "icon/f", "icon/g"];
// delegate
var delegateClass = new MochaJSDelegate();
delegateClass.setHandlerForSelector("tableView:didClickTableColumn:", function(tableView, column) {
log("didClickTableColumn");
log(column);
});
delegateClass.setHandlerForSelector("tableViewSelectionDidChange:", function(notification) {
log("tableViewSelectionDidChange")
var tableView = [notification object]
log([tableView selectedRow]); // print nstableview selected row
});
var delegate = delegateClass.getClassInstance();
// datasource
var dataSourceClass = new MochaJSDelegate();
dataSourceClass.setHandlerForSelector("numberOfRowsInTableView:", function(tableView) {
log("numberOfRowsInTableView");
log(items.length); // print 8
return items.length;
});
dataSourceClass.setHandlerForSelector("tableView:objectValueForTableColumn:row:", function(tableView, tableColumn, row) {
log("objectValueForTableColumn");
log(row); // print null
return NSString.stringWithString("Halo");
});
var dataSource = dataSourceClass.getClassInstance();
var tableContainer = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 240, 198)]
var tableView = [[NSTableView alloc] initWithFrame:NSMakeRect(0, 0, 240, 198)];
var tableColumn = NSTableColumn.alloc().initWithIdentifier("cell");
tableColumn.title = "Symbols";
tableColumn.identifier = "symbols";
tableColumn.width = 240;
tableView.addTableColumn(tableColumn);
tableView.setDataSource_(dataSource);
tableView.setDelegate_(delegate);
tableView.reloadData();
log(tableView.numberOfRows()); // print 2135, not 8
[tableContainer setDocumentView:tableView];
[tableContainer setHasVerticalScroller:true];
[tableContainer setHasHorizontalScroller:false];
[symbolsView addSubview:tableContainer];
So numberOfRowsInTableView:
is being called, but it doesn't seem to affect the number of rows... or does. Have you tried to figure out where 2135
is coming from? Do you still get that result even when you remove the delegate?
So
numberOfRowsInTableView:
is being called, but it doesn't seem to affect the number of rows... or does.
numberOfRowsInTableView:
doesn't seem to affect the number of rows.
Have you tried to figure out where 2135 is coming from? Do you still get that result even when you remove the delegate?
No, i haven't figure out it where 2135 is coming from and i still get that result after remove the delegate
Here's an example of where someone else achieved this: https://github.com/onmyway133/Sketch-Action/blob/7eea7084c6312d851b904d07630b12b601455714/SketchAction.sketchplugin/Contents/Sketch/ui.js
@matt-curtis this example doesn't work or does not rely on Cocoascript. Actually this plugin creates NSTableView by framework (you can comment "@import ui.js" and nothing breaks)
Hmm, so presumably you couldn't get the code in ui.js
to work? I'll have to do some tests in my own time, see if its possible to get this to work. In the mean time, maybe you could give this a try?
Thanks for the answer! I've tried cocoascript-class
but wasn't able to make it work (I can't add it to my plugin properly case I don't have npm
or else). Actually I've done an iteration in Xcode and was managed to run Xcode Storyboard with NSTalbeView from Sketch but Xcode knows nothing about Sketch headers so I've dropped this idea.
@timurnurutdinov You can pull in Sketch's Headers to solve that.