我以爲我會用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之間變化,並且我的代碼不會顯示正確的詳細信息頁面。
我不知道我在這裏錯過了什麼。任何人都可以請幫我嗎?
我看到您正在使用'didDeselectRowAtIndexPath'。你是不是指'didSelectRowAtIndexPath'? – marosoaie 2015-04-06 08:56:58
Segue標識符與window2&window3相同。對於window3你也想顯示window2的細節?你的意思是didSelectRowAtIndexPath? – 2015-04-06 08:59:30
謝謝你。當彈出自動填充窗口時,我選擇了錯誤的方法....我的意思是使用didSelectRowAtIndexPath修復了我的問題。 – Woody 2015-04-06 13:22:54