View in NSTableView in Swift - how

I have an NSTableView whose cells are based on views .

DataSource and Delegate are connected, but I am not able to display the string value of a textField cell .

This is the code in Objective-C that works :

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { return 10; } - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { NSTableCellView *cell = [tableView makeViewWithIdentifier:@"List" owner:self]; [cella.textField setStringValue:"Hey, this is a cell"]; return cell; } 

And here is my code in Swift, not working :

 func numberOfRowsInTableView(aTableView: NSTableView!) -> Int { return 10 //Casual number } func tableView(tableView: NSTableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> NSTableCellView! { var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView! // setup cell without force unwrapping it cell.textField.stringValue = "Hey, this is a cell" println("Method called") //Never printed return cell } 

This is the result: (table on the right side of the image)

Please note that the comment //setup cell without force unwrapping it does not make sense, I forgot to delete it.

Code + ResultTablevie settingsDelegate and DataSource connected

What am I missing?

Change I tried even the following without success:

 func numberOfRowsInTableView(aTableView: NSTableView!) -> Int { return 10 } func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject { var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView cell.textField.stringValue = "Hey this is a cell" return cell; } 

Thanks to everyone. Alberto

+6
source share
4 answers

After hours of searching, I discovered this method that works!

 func tableView(tableView: NSTableView, viewForTableColumn: NSTableColumn, row: Int) -> NSView { var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView cell.textField.stringValue = "Hey, this is a cell" return cell; } 
+11
source

I see that you found your answer by yourself, but from what I see, your hint was in the return value Objective -C.

 - (NSView *)tableView:... 

The return value is NSView.

But you should look at Swift / Objective -c documentaion .

From Documents:

Providing views for rows and columns Tableview: viewForTableColumn: row:

 Asks the delegate for a view to display the specified row and column. Declaration SWIFT @optional func tableView(_ tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row row: Int) -> NSView! OBJECTIVE-C - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 

Pay attention to โ†’ NSView! in fast code.

New documents let you see the code for Swift and Objective -c side by side or this or that. You can use the selection tab at the top of the documentation to select.

It also looks like your code should include a "!" for options

+2
source

If you change the content mode of the table view from the view based on the cell on which the method is called.

enter image description here

+2
source

It beat me for an hour. Works with Swift 2.2, probably won't work for earlier or later versions:

  let cell = tableView.makeViewWithIdentifier(myid!, owner: nil) // You can't cast this with as! like they want you to if let mycell = cell as? NSTableCellView { mycell.textField?.stringValue = text return mycell } 
0
source

Source: https://habr.com/ru/post/971156/


All Articles