2015-10-19 30 views
0

我試圖在UITableViewController出現(通過segue)時選擇第一行。在Swift中選擇UITableViewController中的第一行

我使用這是我從我以前的目標C項目繼承了下面的代碼:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 
    { 
     let customCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! IGANavlistTableCustomViewCell 

     // Code that sets the custom view cell 

     // Then... 
     // Selecting the first row by default 
     if(indexPath.row == 0) 
     { 
      let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0) 

      tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None) 

      tableView.delegate?.tableView!(tableView, didSelectRowAtIndexPath: rowToSelect) 
     } 

     return customCell; 
    } 

我收到以下錯誤:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[IGANavlistTableVC tableView:didSelectRowAtIndexPath:]: unrecognized selector sent to instance 0x79e21040' 

我將不勝感激,如果有人可以提供幫助。

謝謝!

編輯:

這是我didDeselectRowAtIndexPath方法:

var navaidSelected = [String]() 
var listOfNavPoints = [[String]]() 

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) 
    { 
     // The selected object of the Array with all navaids data (i.e., the selected navaid) is converted to a string 
     self.navaidSelected = self.listOfNavPoints[indexPath.row] 
    } 
+0

你在使用故事板嗎?一切都正確嗎? –

+0

錯誤指向didSelectRowAtIndexPath。你能爲此顯示代碼嗎? –

回答

4

你忘了實現func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)方法。

而且你不需要使用delegate時調用didSelectRowAtIndexPath

if(indexPath.row == 0) 
     { 
      let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0) 

      tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None) 

      self.tableView(tableView, didSelectRowAtIndexPath: rowToSelect) 
     } 

=== EDITED ===

更改didDeselectRowAtIndexPathdidSelectRowAtIndexPath

+1

謝謝。對不起,我沒有將它包含在原始信息中。糾正。此外,讓你的方式給我以下錯誤:「不能調用非函數類型'UITableView'的值」。 –

+0

謝謝。改變。沒有編譯錯誤,但仍然是運行時錯誤。有趣的是,如果我選擇第二行然後選擇第一行,則不會發生錯誤。 –

+0

關於此問題的興奮點在這裏:http://stackoverflow.com/questions/2106292/uitableview-didselectrowatindexpath-not-being-called-on-first-tap – t4nhpt

相關問題