MochaJSDelegate icon indicating copy to clipboard operation
MochaJSDelegate copied to clipboard

Can't set row count in NSTableViewDataSource using MochaJSDelegate

Open alhazme opened this issue 6 years ago • 9 comments

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

alhazme avatar Aug 02 '17 03:08 alhazme

Can you share the code you're using?

matt-curtis avatar Aug 02 '17 11:08 matt-curtis

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];

alhazme avatar Aug 02 '17 14:08 alhazme

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?

matt-curtis avatar Aug 03 '17 01:08 matt-curtis

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

alhazme avatar Aug 03 '17 04:08 alhazme

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 avatar Aug 03 '17 10:08 matt-curtis

@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)

timurnurutdinov avatar Oct 11 '17 20:10 timurnurutdinov

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?

matt-curtis avatar Oct 12 '17 00:10 matt-curtis

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 avatar Oct 19 '17 15:10 timurnurutdinov

@timurnurutdinov You can pull in Sketch's Headers to solve that.

matt-curtis avatar Jan 19 '18 03:01 matt-curtis