简体   繁体   English

PrepareForSegue 与 UITableViewController - 传递数据

[英]PrepareForSegue with UITableViewController - Pass data

I am having a huge problem with my code.我的代码有一个大问题。 My question is, how can I get the data from the tableviewcell the user clicks on to a detail viewcontroller.我的问题是,如何从用户单击的 tableviewcell 中获取数据到详细视图控制器。

I am already getting the data out of an array and displaying it in my tableviewcontroller, but how can I pass that data through to a new viewcontroller using prepareForSegue?我已经从数组中获取数据并将其显示在我的 tableviewcontroller 中,但是如何使用 prepareForSegue 将该数据传递给新的 viewcontroller?

This is my code for displaying data in the cells.这是我在单元格中显示数据的代码。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "MealTableViewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MealTableViewCell

    // Fetches the appropriate meal for the data source layout.
    let meal = meals[indexPath.row]

    cell.nameLabel.text = meal.name
    cell.photoImageView.image = meal.photo
    cell.descriptionLabel.text = meal.description
    cell.servedWithLabel.text = meal.servedWith

    return cell
}

The problem is, that calling a prepareForSegue outside of that means that I cannot use for instance meal.name to pass the name through to the detail viewcontroller?问题是,在这之外调用 prepareForSegue 意味着我不能使用例如 meal.name 将名称传递给详细信息视图控制器? What to do?怎么办?

Hope you guys can help me - I have been struggling with this for some time now :-(希望你们能帮助我 - 我已经为此苦苦挣扎了一段时间:-(

As pointed out by Arun, you need to use -prepareForSegue .正如 Arun 所指出的,您需要使用-prepareForSegue Here's what I'd do to pass something to your detail view controller (say meal.name )这是我将某些内容传递给您的详细信息视图控制器的方法(例如meal.name

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Use this to determine the selected index path. You can also store the selected index path in a variable using -didSelectRowAtIndexPath
    let cell = sender as! UITableViewCell
    let indexPath = self.tableView.indexPathForCell(cell)

    // Get the relevant detail
    let meal = meals[indexPath.row]

    // Create reference and pass it
    let detailViewController = segue.destinationViewController as! MyDetailViewController
    detailViewController.mealName = meal.name

}

And eventually, just hook up the UITableViewCell with MyDetailViewController using your Storyboard (and select whatever presentation and transition style you prefer)最后,只需使用您的StoryboardUITableViewCellMyDetailViewController连接起来(并选择您喜欢的任何演示和过渡样式)

Edited for Swift 4为 Swift 4 编辑

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let cell = sender as! UITableViewCell
    let indexPath = self.tableView.indexPath(for: cell)

    // Get the relevant detail
    let meal = meals[indexPath!.row]

    // Create reference and pass it
    let detailViewController = segue.destination as! MyDetailViewController
    detailViewController.mealName = meal.name
}

When you click a cell.当您单击一个单元格时。 It will call didSelectRow .它将调用didSelectRow In didSelectRow save the indexPath.row and do performseguewithidentifierdidSelectRow保存indexPath.rowperformseguewithidentifier indexPath.row

Now in prepareForSegue get the object from the array using meals[selectedIndex] and assign to detailViewController Objects .现在在prepareForSegue使用meals[selectedIndex]从数组中获取对象并分配给detailViewController Objects

Make sure segue is from ViewController to ViewController .确保segue是从ViewControllerViewController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM