我想试试用swift构建一个ipad应用程序。我正在使用的应用程序是一个大师级的细节应用程序。在主表中,我有两行:"Window1"one_answers"Window2"以及两个详细视图。我已经创建了一个故事板分段到两个详细视图(1是默认视图)。这两个片段被称为"showDetail"one_answers"showWindow2"。
我在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之间变化,并且我的代码不会显示正确的详细信息页。
我不知道我在这里错过了什么。有人能帮我吗?
您的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(如果您按第一行,将使用该Segue)。
将case 2
Segue更改为将显示您的第三个详细视图的Segue应该可以解决您的问题
此外,您可以使用case 0
查看是否有人按下了您的第一排。尽管您的解决方案可以工作,但如果按下第一行,它将激发您的default
语句,如果您有case 0
,如果您按下第一行而不是默认值,它将被激发。这将导致default
捕获任何错误