如何访问 prepareForSegue 中选定单元格的数据

How do I gain access to data from the selected cell in prepareForSegue

我正在尝试创建我的第一个 segue,我想将数据从我选择的 tableViewCell 传递到我正在 segue 的辅助视图控制器。

我的问题是:

由于 prepareForSegue 不采用 indexPath,我如何获取有关我选择的特定单元格的信息?

这是我的应用程序的照片,里面有一些虚拟数据,右边带箭头的单元格代表我要从中提取的单元格:

这是我的 CustomCell class,这些行派生自:

TimingCell.swift

class TimingCell: UITableViewCell {

    @IBOutlet weak var timingLabel: UILabel!
    @IBOutlet weak var timeSetLabel: UILabel!

    ...
}

这里是我设置 segue 方法的地方:

ViewController.Swift

class ViewController: UITableViewController, UITableViewDataSource, UITableViewDelegate, SettingCellDelegate {


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


    var view: TimeViewController = segue.destinationViewController as! TimeViewController


}

*如果您需要在下面的评论中查看更多代码,请告诉我

你可以这样得到IndexPath

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


   var view = segue.destinationViewController as! TimeViewController
   if let indexPath = myTableView.indexPathForSelectedRow() {
            view.tempString = indexPath.row.description
        }

}

所以你应该从 UITableViewController 开始,而不仅仅是视图控制器。

在 UITableViewController 中,您需要覆盖:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete method implementation.
    // Return the number of rows in the section.

    return 6 //Looks like you have six rows in your TableView
}

现在您需要一个 class 范围的 NSIndexPath 变量来跟踪 selected 行的索引,因此要创建变量:

var path = NSIndexPath()

现在您还需要两个覆盖,第一个是在 selected 行时设置路径变量,第二个是删除 select 您 select 之后的行(确保当您从子视图控制器转换回 table 视图时不播放 deselect 动画:

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath : NSIndexPath) -> NSIndexPath? {
    path = indexPath
    return indexPath
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.deselectRowAtIndexPath(path, animated: true)
}

最后,在您的 prepareForSegue 函数中,您可以使用路径变量来确定哪一行被 selected 并相应地设置子视图控制器变量:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var dest = segue.destinationViewController as! TimeViewController
    if path == 1 {
        //set some destination variables here
    } else {
        //or here
    }
}

并且如果您有一个数据结构,您可以根据该数据结构确定 table 视图中的每一行将包含什么,只需使用路径作为数据结构的索引并将该行的信息传递到目的地视图控制器。例如,类似于:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var dest = segue.destinationViewController as! TimeViewController
    dest.rowTitle = myRowsArray[path.row].title
}

Table浏览量有点习惯了...祝你好运!

您可以在 tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 函数中设置一些变量。在您尝试访问的视图中设置相同的变量。在 prepareForSegue 函数中设置值,它将起作用。

以下是我在申请中使用的代码。我在 didSelectRowAtIndexPath 函数中设置了 roomUUID,并在 prepareForSegue 函数中传递它。目标 segue 视图控制器中也有一个 roomUUID 变量。

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    println ("Select Row At: \(indexPath.item)")
    // Need to show the view for room information.
    var roomInfo: NSMutableDictionary = self.roomList[indexPath.item] as NSMutableDictionary
    self.roomUUID = roomInfo["uuid"] as String
    self.performSegueWithIdentifier("ShowRoomInfo", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        if let roomInfoViewController = segue.destinationViewController as? RoomInfoViewController {
            if let identifier = segue.identifier{
                if identifier == "ShowRoomInfo"{
                    roomInfoViewController.roomUUID = self.roomUUID
                    println ("Show Room Info")
                }
            }
        }
    }

希望对您有所帮助。 :)

在 prepare(for segue:) 方法中,使用这个:

guard let indexPathOfSelectedRow = tableView.indexPathForSelectedRow else {
    fatalError("Unable to determine which cell is selected")
}

然后你可以得到这样的索引:

indexPathOfSelectedRow.row