繁体   English   中英

设置NSTableView单元格文本

[英]Setting NSTableView cell text

我有一个NSTableView,我想填充20个单元格,每个单元格都会说“Test”。 我能熟练使用UITableView,但与NSTableViews不同,所以我去网上搜索了这个。 哦,这让我很困惑! 我知道我需要使用numberOfRowsInTableView函数,但是如何设置单元格的文本? 我找到的每个来源似乎都以不同的方式做所有事情。 例如, 网站使用:

func tableView(tableView: NSTableView!, viewForTableColumn tableColumn: NSTableColumn!, row: Int) -> NSView! {
// 1
var cellView: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn.identifier, owner: self) as NSTableCellView

// 2
if tableColumn.identifier == "BugColumn" {
  // 3
  let bugDoc = self.bugs[row]
  cellView.imageView!.image = bugDoc.thumbImage
  cellView.textField!.stringValue = bugDoc.data.title
  return cellView
}

return cellView

}

我尝试了但是我收到了一个错误 - 在打开一个可选项时,代码找到了nil。 然后我尝试了我在这里找到的东西

func tableView(tableView: NSTableView, dataCellForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSCell? {
if tableColumn == nil { return nil }
if tableColumn!.identifier != "right" { return nil }
let cell = NSPopUpButtonCell()
cell.bordered = false
cell.menu!.addItemWithTitle("one", action: nil, keyEquivalent: "")
cell.menu!.addItemWithTitle("two", action: nil, keyEquivalent: "")
cell.menu!.addItemWithTitle("three", action: nil, keyEquivalent: "")
cell.selectItemAtIndex(1) // <--- obviously ignored ?!
return cell

}

所以我的问题是,如何设置单元格文本? 我上面插入的两个例子与他们的作用有何不同? 请你理解这一点 - 因为我肯定不能!

谢谢,一个困惑的CodeIt

PS我看了几个其他来源,除了上面提到的两个。 我只是很困惑..

编辑:在第一个示例中提到的解包可选错误时找到的nil在此行中找到:

var cellView: NSTableCellView = tableView.makeViewWithIdentifier(tableColumn.identifier, owner: self) as NSTableCellView
// Get an existing cell with the MyView identifier if it exists

var cellView?: NSTableCellView = tableView.makeViewWithIdentifier("someIdentifier", owner: self) as NSTableCellView

// There is no existing cell to reuse so create a new one

if cellView == nil {
    cellView = NSTableCellView(frame: NSRect())
// The identifier of the NSTextField instance is set to someIdentifier.
// This allows the cell to be reused.
     cellView.identifier = "someIdentifier
}

这应该给你单元格,你可以继续。 有关更多信息,请参阅Apple Doc

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM