Swift如何从CollectionviewCell中删除indexPath值?

时间:2017-01-11 02:53:49

标签: ios swift segue uisplitviewcontroller

我希望能够在DetailViewController.中重新加载值。如何对所选的indexPath进行预设,以便在下一个视图控制器中调用该值?

didSelectItemAt我有:

let monsters: Monsters!

let mons: [Monsters] = Data().mons
monsters = mons[indexPath.row] 

self.performSegue(withIdentifier: "DetailVC", sender: monsters)

1 个答案:

答案 0 :(得分:3)

您可以在monster中创建DetailViewController媒体资源,然后在致电prepare(for segue:)

时指定该媒体资源
// This would be a property at the top of DetailViewController
var monster:Monster?

然后在执行segue的VC内部,为selectedMonster创建一个基于indexPath分配的属性:

var selectedMonster:Monster?

然后当您用户选择一个单元格时:

selectedMonster = mons[indexPath.row] 

在同一个VC中,调用performSegue(withIdentifier:)时会调用此方法:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DetailVC" {
        let detailVC = segue.destination as! DetailViewController
        detailVC.monster = selectedMonster
    }
}

您可能想要查看performSegue(withIdentifier:)的来电,通常您希望发件人为self,即:

performSegue(withIdentifier: "DetailVC", sender: self)