在NSTableViewCell上聚焦NSTextField

问题描述:

目前我正在为macOS开发一个应用程序。 我有一个NSTableView与不同的行。一行由NSTextField和一个按钮组成。 您可以在文本框中输入一个ID,并且一旦按下按钮,就会从服务器加载数据。在NSTableViewCell上聚焦NSTextField

但我有一个问题。我无法真正关注文本字段控件。每当我点击它时,细胞就会集中注意力,而且会“吃”消息。

当我点击控件时,消息被吃掉了,但是当我“拖动”控件中的文本时,我可以将它聚焦。 我不想让用户关注的细胞,所以我采取了以下事件处理程序:

func tableView(tableView: NSTableView, shouldSelectRow row: Int) -> Bool 
{ 
    if tableView == self.tableViewSegments 
    { 
     if segments[row].id!.integerValue < 0 
     { 
      return false 
     } 
    } 

    return true 
} 

这可以防止集中在电池,而且文本框,按钮仍然有效,但? 一旦我禁用这种方法,我可以集中单元格而不是文本字段(请参阅图像)。

http://i.stack.imgur.com/0er99.png

希望你能够理解的问题是什么。 我该如何达到理想的行为?

刚刚发现一个解决办法自己:

func tableViewSelectionDidChange(notification: NSNotification) 
    { 
     if notification.object != nil 
     { 
      let tableView = notification.object as? NSTableView 
      if tableView != nil && tableView!.selectedRow == 1 
      { 
       let cell = tableView?.viewAtColumn(0, row: tableView!.selectedRow, makeIfNecessary: false) as? InstallSegmentTextFieldCell 

       if cell != nil 
       { 
        // Set the focus to the textfield. 
        cell!.stravaId.becomeFirstResponder() 
        tableView!.deselectAll(nil) 
       } 
      } 
     } 
    }