gpt4 book ai didi

swift - NSTableView : replace empty table with image

转载 作者:行者123 更新时间:2023-11-30 13:17:04 31 4
gpt4 key购买 nike

当我的 NSTableView 中没有任何行可显示时,我想显示图像。我在自定义 NSViewController 类中尝试了以下操作,但仍然看到一个空表。我在 Assets.xcassets 中有“ic_empty_image”。

func numberOfRowsInTableView(tableView: NSTableView) -> Int {
if model.numRows == 0 {
let image = NSImage(named: "ic_empty_image")
let imageView = NSImageView()
imageView.image = image
self.view.replaceSubview(tableView, with: imageView)
self.view.setNeedsDisplayInRect(imageView.visibleRect)
} else {
return model.numRows
}
}

最佳答案

如果您利用基于 View 的表格,当您知道有空数据集时,只需翻转单元格的 View 即可。诀窍是将表绑定(bind)到数组,而不是让它为空,只包含一项 - 指示器。

我在这里为您创建了示例项目: https://github.com/emankovski/nstableview-empty-picture

但一般逻辑实际上非常简单:我在 nib 中创建了 NSTableCellView 对象,并在看到没有数据的指示符时返回它。请注意,我还更改了 TableView 的行高,因为带有图像的单元格比带有文本的单元格高。我希望它有帮助。

 class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource {

private static let placeholder = "<Placeholder>"
dynamic var items = [String]()

@IBOutlet weak var window: NSWindow!
@IBOutlet weak var tableView: NSTableView!
@IBOutlet weak var imageCell: NSTableCellView!

@IBAction func addRowsPressed(sender: AnyObject) {
appendItems()
}

@IBAction func clearTablePressed(sender: AnyObject) {
removeItems()

}

func applicationDidFinishLaunching(aNotification: NSNotification) {
appendItems()

}

func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}

func appendItems() {


items.removeAll()

items.append("One")
items.append("Two")
items.append("Three")

tableView.rowHeight = 20

}

func removeItems() {

items.removeAll()
tableView.rowHeight = 50
items.append(AppDelegate.placeholder)


}

func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {

if items[row] == AppDelegate.placeholder {

return imageCell
}

let cellView = tableView.makeViewWithIdentifier("CellView", owner: nil) as! NSTableCellView

return cellView
}


}

Storyboard更新:如何拖动表格单元格 View 以查看 Storyboard 中的 Controller 的图片:

enter image description here

然后只需创建 socket 并将该对象连接并拖动到该 socket :

  @IBOutlet var tableCellView: NSTableCellView!

关于swift - NSTableView : replace empty table with image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38110137/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com