在主 - 详细信息应用程序中使用Swift,我如何以编程方式切换详细信息视图?

时间:2015-04-06 08:52:26

标签: ios xcode swift master-detail

我以为我会使用swift构建一个ipad应用程序。我正在使用的应用程序是一个主要细节的应用程序。在主表中我有2行: "窗口1"和" Window2"和两个细节视图。我创建了一个故事板segue到两个详细视图(1是默认视图)。两个segues被称为" showDetail"和" 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)
            }
        } 

我认为我遇到的问题是" row"似乎根据我目前在哪一行而改变。我的意思是在调试时,选择" Window1"," Window2"或" Window3"时的行值。似乎在0,1和2之间变化,我的代码不会导致显示正确的详细信息页面。

我不知道我在这里失踪了什么。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分: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更改为将显示您的第3个详细视图的Segue应解决您的问题

此外,您可以使用case 0查看是否有人按下了您的第一行。虽然您的解决方案可行,但如果按下第一行,它将触发default语句,如果您有case 0,如果按第一行而不是默认值,则会触发此操作。这将导致default捕获任何错误

相关问题