2015-04-06 11 views
-1

我以爲我會用swift構建一個ipad應用程序。我正在使用的應用程序是一個主 - 細節應用程序。在主表中,我有兩行: 「Window1」和「Window2」以及兩個詳細視圖。我爲兩個細節視圖創建了一個故事板segue(1是默認視圖)。這兩個段落被稱爲「showDetail」和「showWindow2」。在主從應用程序中使用Swift,我如何以編程方式切換細節視圖?

視頻我在YouTube上觀看用下面的代碼從主選項卡的用戶引導到適當的詳細頁面:

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
     return 1 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 2 
    } 

    override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
     var row = indexPath.row 

     switch row 
     { 
     case 1: 
      self.performSegueWithIdentifier("showDetail", sender: self) 
     default: 
      self.performSegueWithIdentifier("showWindow2", sender: self) 
     } 
    } 

其工作時,我有在母版頁只有兩排。 上面的代碼似乎並不當我有母版頁上3行工作(通過添加一行標籤「Window3」):

override func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
      return 1 
     } 

     override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
      return 3 
     } 

     override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) { 
      var row = indexPath.row 

      switch row 
      { 
      case 1: 
       self.performSegueWithIdentifier("showDetail", sender: self) 
      case 2: 
       self.performSegueWithIdentifier("showWindow2", sender: self) 
      default: 
       self.performSegueWithIdentifier("showWindow2", sender: self) 
      } 
     } 

我想我遇到的問題是,值的「行」似乎改變取決於我目前在哪一行。我的意思是當我調試時,選擇「Window1」,「Window2」或「Window3」時的行值似乎在0,1和2之間變化,並且我的代碼不會顯示正確的詳細信息頁面。

我不知道我在這裏錯過了什麼。任何人都可以請幫我嗎?

+2

我看到您正在使用'didDeselectRowAtIndexPath'。你是不是指'didSelectRowAtIndexPath'? – marosoaie 2015-04-06 08:56:58

+1

Segue標識符與window2&window3相同。對於window3你也想顯示window2的細節?你的意思是didSelectRowAtIndexPath? – 2015-04-06 08:59:30

+0

謝謝你。當彈出自動填充窗口時,我選擇了錯誤的方法....我的意思是使用didSelectRowAtIndexPath修復了我的問題。 – Woody 2015-04-06 13:22:54

回答

0

您的switch語句似乎是錯誤的。

switch row{ 
    case 1: 
     self.performSegueWithIdentifier("showDetail", sender: self) 
    case 2: 
     self.performSegueWithIdentifier("showWindow2", sender: self) 
    default: 
     self.performSegueWithIdentifier("showWindow2", sender: self) 
} 

在您的switch語句中使用,所使用的默認行(如果按第一排將被使用)self.performSegueWithIdentifier(「showWindow2」, sender: self)可以使用相同的Segue公司。

改變case 2 Segue公司的Segue公司,會告訴你的第三個細節視圖應該解決您的問題

此外,你還可以使用case 0,看看是否有人按下你的第一行。雖然你的解決方案可以工作,但如果第一行被按下,它將觸發你的default聲明,如果你有case 0,如果你按下第一行而不是默認值,這將被觸發。這將導致default發現任何錯誤

相關問題