Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't set row count in NSTableViewDataSource using MochaJSDelegate #10

Open
alhazme opened this issue Aug 2, 2017 · 9 comments
Open

Comments

@alhazme
Copy link

alhazme commented Aug 2, 2017

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

@matt-curtis
Copy link
Owner

Can you share the code you're using?

@alhazme
Copy link
Author

alhazme commented Aug 2, 2017

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

@matt-curtis
Copy link
Owner

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?

@alhazme
Copy link
Author

alhazme commented Aug 3, 2017

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

@matt-curtis
Copy link
Owner

@timurnurutdinov
Copy link

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

@matt-curtis
Copy link
Owner

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?

@timurnurutdinov
Copy link

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.

@matt-curtis
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants