一个视图控制器上的多个 NSTableView
Multiple NSTableViews on one View Controller
我想在一个 ViewController 上有多个 NSTableView。我已经使用以下代码实现了第一个 TableView:
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
}
}
这个 NSTableView 工作正常,但它显然与视图控制器紧密相关。添加第二个 NSTableView 的最佳方法是什么?
这取决于您对 "best" 的理解。最简单的是仅根据回调中的 tableView 参数进行推理,即
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
if(tableView == self.tableView1)
{
}
else if(tableView == self.tableView2)
{
}
}
但我建议创建单独的 classes 用作数据源。 classes 将实现 UITableViewDataSource 协议。您创建 class 的两个实例并相应地绑定您的 table 视图。
终于知道怎么做了。在我看来,这比将 NSTableView 放在主控制器中要好得多。
每个 table 都定义了自己的 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是相同的,但显然可以有不同的数据等
我的 table 是基于视图的,所以这里是单元格视图 class 定义:
import Cocoa
class MyCellView: NSTableCellView
{
@IBOutlet weak var itemName: NSTextField!
@IBOutlet weak var itemDescription: NSTextField!
}
这是视图控制器方法:
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")
}
}
我想在一个 ViewController 上有多个 NSTableView。我已经使用以下代码实现了第一个 TableView:
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
}
}
这个 NSTableView 工作正常,但它显然与视图控制器紧密相关。添加第二个 NSTableView 的最佳方法是什么?
这取决于您对 "best" 的理解。最简单的是仅根据回调中的 tableView 参数进行推理,即
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
if(tableView == self.tableView1)
{
}
else if(tableView == self.tableView2)
{
}
}
但我建议创建单独的 classes 用作数据源。 classes 将实现 UITableViewDataSource 协议。您创建 class 的两个实例并相应地绑定您的 table 视图。
终于知道怎么做了。在我看来,这比将 NSTableView 放在主控制器中要好得多。
每个 table 都定义了自己的 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是相同的,但显然可以有不同的数据等
我的 table 是基于视图的,所以这里是单元格视图 class 定义:
import Cocoa
class MyCellView: NSTableCellView
{
@IBOutlet weak var itemName: NSTextField!
@IBOutlet weak var itemDescription: NSTextField!
}
这是视图控制器方法:
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")
}
}