I want to have multiple NSTableViews on one ViewController. I have implemented the first TableView with the following code:
class MainViewController: NSViewController
{
@IBOutlet weak var tableView: NSTableView!
override func viewDidLoad()
{
super.viewDidLoad()
let nib = NSNib(nibNamed: "MyCellView", bundle: NSBundle.mainBundle())
tableView.registerNib(nib!, forIdentifier: "MyCellView")
let appDelegate = NSApplication.sharedApplication().delegate as! AppDelegate
appDelegate.view = self
}
}
extension MainViewController: NSTableViewDataSource, NSTableViewDelegate
{
func numberOfRowsInTableView(tableView: NSTableView) -> Int
{
return 5
}
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
return 25
}
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier("MyCellView", owner: self) as! MyCellView
cell.itemName.stringValue = "row text"
return cell
}
}
This NSTableView is working fine but it is obviously tied closely with the view controller. What is the best way of adding a second NSTableView?
Finally found out how to do this. This is a much nicer approach than putting the NSTableView in the main controller in my opinion.
Each table is defined with its own class:
import Cocoa
class Table1: NSTableView, NSTableViewDataSource, NSTableViewDelegate
{
override func drawRect(dirtyRect: NSRect)
{
super.drawRect(dirtyRect)
}
func numberOfRowsInTableView(tableView: NSTableView) -> Int
{
return 10
}
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
return 50
}
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier("MyCellView", owner: self) as! MyCellView!
cell.itemName.stringValue = "hello"
cell.itemDescription.stringValue = "This is some more text"
return cell
}
}
Table 2 is the same, but can obviously have different data etc.
My tables were View Based so here is the cell view class definition:
import Cocoa
class MyCellView: NSTableCellView
{
@IBOutlet weak var itemName: NSTextField!
@IBOutlet weak var itemDescription: NSTextField!
}
Here is the View Controller method:
import Cocoa
class ViewController: NSViewController
{
var dataSource1 : Table1!
var dataSource2 : Table2!
@IBOutlet weak var table1: NSTableView!
@IBOutlet weak var table2: NSTableView!
override func viewDidLoad()
{
super.viewDidLoad()
self.dataSource1 = Table1()
table1.setDataSource(self.dataSource1)
table1.setDelegate(self.dataSource1)
self.dataSource2 = Table2()
table2.setDataSource(self.dataSource2)
table2.setDelegate(self.dataSource2)
let nib = NSNib(nibNamed: "MyCellView", bundle: NSBundle.mainBundle())
table1.registerNib(nib!, forIdentifier: "MyCellView")
table2.registerNib(nib!, forIdentifier: "MyCellView")
}
}
That depends on what do you mean with "best". The easiest is to just reason based on the tableView parameter in the callbacks, i.e.
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
if(tableView == self.tableView1)
{
}
else if(tableView == self.tableView2)
{
}
}
But I'd suggest to create separate classes to be used as datasources. That classes would implement UITableViewDataSource protocol. You create two instances of that class and tie your table views accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With