【问题标题】:View-Based NSTableView in Swift - How toSwift 中基于视图的 NSTableView - 如何
【发布时间】:2014-08-12 08:07:39
【问题描述】:

我有一个 NSTableView,其单元格是基于视图的

DataSource 和 Delegate 已连接,但我无法显示单元格的 textField 字符串值

这是 Objective-C 中的代码,正在运行

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {

return 10;

}

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

        NSTableCellView *cell = [tableView makeViewWithIdentifier:@"List" owner:self];
        [cella.textField setStringValue:"Hey, this is a cell"];

        return cell;
}

这是我在 Swift 中的代码,无法正常工作

func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
{
    return 10 //Casual number
}
func tableView(tableView: NSTableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> NSTableCellView! {
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView!
    // setup cell without force unwrapping it
    cell.textField.stringValue = "Hey, this is a cell"
    println("Method called") //Never printed
    return cell
}

这是结果:(图片右侧的表格)

注意//setup cell without force unwrapping it的评论没有意义,我忘记删了。

我错过了什么?

编辑:我什至尝试了以下但没有成功:

func numberOfRowsInTableView(aTableView: NSTableView!) -> Int
{
    return 10
}
func tableView(tableView: NSTableView!, objectValueForTableColumn tableColumn: NSTableColumn!, row: Int) -> AnyObject
{
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
    cell.textField.stringValue = "Hey this is a cell"
    return cell;
}

谢谢大家。 阿尔贝托

【问题讨论】:

  • 在 Objective-C 代码中你有 tableView:viewForTableColumn:row:,而在 Swift 中你有 tableView(tableView, cellForRowAtIndexPath)。肯定有 tableView:viewForTableColumn:row: Swift 等价物?

标签: tableview swift cell


【解决方案1】:

经过几个小时的搜索,我发现这种方法有效!

func tableView(tableView: NSTableView, viewForTableColumn: NSTableColumn, row: Int) -> NSView
{
    var cell = tableView.makeViewWithIdentifier("List", owner: self) as NSTableCellView
    cell.textField.stringValue = "Hey, this is a cell"
    return cell;
}

【讨论】:

    【解决方案2】:

    我看到您自己找到了答案,但据我所知,您的线索在 Objective -C 委托的返回值中。

    - (NSView *)tableView:...
    

    返回值是一个 NSView。

    但你应该看看Swift/Objective -c documentaion

    来自文档:

    为行和列提供视图 表视图:viewForTableColumn:行:

    Asks the delegate for a view to display the specified row and column.
    
    Declaration
    SWIFT
    @optional func tableView(_ tableView: NSTableView!,
          viewForTableColumn tableColumn: NSTableColumn!,
                         row row: Int) -> NSView!
    OBJECTIVE-C
    - (NSView *)tableView:(NSTableView *)tableView
       viewForTableColumn:(NSTableColumn *)tableColumn
                      row:(NSInteger)row
    

    注意 -> NSView!也在 swift 代码中。

    新文档允许您并排查看 Swift 和 Objective -c 的代码。您可以使用文档顶部的选择选项卡进行选择。

    您的代码似乎也应该包含“!”对于选项

    【讨论】:

    • 感谢您的回答,它真的帮助了我:)
    【解决方案3】:

    如果您将表格视图的内容模式从基于视图更改为基于单元格,则会调用该方法。

    【讨论】:

      【解决方案4】:

      这让我痛了一个小时。适用于 Swift 2.2,可能不适用于早期或更高版本:

       let cell = tableView.makeViewWithIdentifier(myid!, owner: nil)  // You can't cast this with as! like they want you to
      
          if let mycell = cell as? NSTableCellView {
              mycell.textField?.stringValue = text
              return mycell
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        相关资源
        最近更新 更多