无法将 'NSTableCellView' 类型的值转换为 'NSTableViewTest'
Could not cast value of type 'NSTableCellView' to 'NSTableViewTest'
我正在尝试为 NSTableView 设置一个自定义 class 并且似乎几乎已经完成了。
当我 运行 此代码时,我收到一条错误消息:无法将 'NSTableCellView' (0x7fff7a859af0) 类型的值转换为 'NSTableViewTest.MyCellView' (0x10001e480) 行中的 "let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! MyCellView!"
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 25
}
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! MyCellView!
return cell
}
}
我的单元格 class 定义为:
import Cocoa
class MyCellView: NSView
{
@IBOutlet weak var itemName: NSTextField!
@IBOutlet weak var itemDescription: NSTextField!
@IBOutlet weak var itemImage: NSImageView!
}
您的 MyCellView
class 属于 NSView
,将其更改为 NSTableCellView
类型,因为您无法将 NSView
转换为 NSTableCellView
.
希望对你有所帮助!
问题出在您使用的标识符上。 tableColumn!.identifier
这是一个列的标识符,但您想为单元格视图生成一个 NSView。
1. Go to the storyboard and select a cell view
2. associate it with MyCellView (set it in the identity inspector)
3. specify an identifier in the storyboard (set it in the identity inspector)
4. Modify the identifier in the code. It should be some string like "MyCellView"
tableColumn!.identifier 应该是一个 String ,例如。 "cell"。
在 Storyboard 中选择 Prototype 单元格后, String 应该匹配标识符。
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier("cell", owner: self) as! MyCellView!
return cell
}
您可能还需要在 ViewController 中注册 nib 文件,这可以在 viewDidLoad()
中完成。
let nib = NSNib(nibNamed: "MyCellView", bundle: Bundle.main)
myTableView.register(nib!, forIdentifier: "MyCellView")
我正在尝试为 NSTableView 设置一个自定义 class 并且似乎几乎已经完成了。
当我 运行 此代码时,我收到一条错误消息:无法将 'NSTableCellView' (0x7fff7a859af0) 类型的值转换为 'NSTableViewTest.MyCellView' (0x10001e480) 行中的 "let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! MyCellView!"
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 25
}
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! MyCellView!
return cell
}
}
我的单元格 class 定义为:
import Cocoa
class MyCellView: NSView
{
@IBOutlet weak var itemName: NSTextField!
@IBOutlet weak var itemDescription: NSTextField!
@IBOutlet weak var itemImage: NSImageView!
}
您的 MyCellView
class 属于 NSView
,将其更改为 NSTableCellView
类型,因为您无法将 NSView
转换为 NSTableCellView
.
希望对你有所帮助!
问题出在您使用的标识符上。 tableColumn!.identifier
这是一个列的标识符,但您想为单元格视图生成一个 NSView。
1. Go to the storyboard and select a cell view
2. associate it with MyCellView (set it in the identity inspector)
3. specify an identifier in the storyboard (set it in the identity inspector)
4. Modify the identifier in the code. It should be some string like "MyCellView"
tableColumn!.identifier 应该是一个 String ,例如。 "cell"。 在 Storyboard 中选择 Prototype 单元格后, String 应该匹配标识符。
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView?
{
let cell = tableView.makeViewWithIdentifier("cell", owner: self) as! MyCellView!
return cell
}
您可能还需要在 ViewController 中注册 nib 文件,这可以在 viewDidLoad()
中完成。
let nib = NSNib(nibNamed: "MyCellView", bundle: Bundle.main)
myTableView.register(nib!, forIdentifier: "MyCellView")