2016-07-23 57 views
1

目前我正在爲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

希望你能夠理解的問題是什麼。 我該如何達到理想的行爲?

回答

1

剛剛發現一個解決辦法自己:

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) 
       } 
      } 
     } 
    }