Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UITableViewController进行PrepareForSegue-传递数据_Ios_Swift_Uitableview_Tableviewcell - Fatal编程技术网

Ios 使用UITableViewController进行PrepareForSegue-传递数据

Ios 使用UITableViewController进行PrepareForSegue-传递数据,ios,swift,uitableview,tableviewcell,Ios,Swift,Uitableview,Tableviewcell,我的代码有一个很大的问题。我的问题是,如何从用户单击的tableviewcell中获取数据到详细视图控制器 我已经从数组中获取数据并将其显示在我的tableviewcontroller中,但是如何使用prepareForSegue将数据传递到新的viewcontroller 这是我在单元格中显示数据的代码 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&

我的代码有一个很大的问题。我的问题是,如何从用户单击的tableviewcell中获取数据到详细视图控制器

我已经从数组中获取数据并将其显示在我的tableviewcontroller中,但是如何使用prepareForSegue将数据传递到新的viewcontroller

这是我在单元格中显示数据的代码

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
}
问题是,在该范围之外调用prepareForSegue意味着我不能使用例如mean.name将名称传递给detail viewcontroller?怎么办


希望你们能帮助我-我已经为此挣扎了一段时间:-(

当你点击一个单元格时。它将调用
didSelectRow
。在
didSelectRow
中保存
indexPath.row
并使用标识符执行
performsguewithidentifier

现在在
prepareForSegue
中,使用
fines[selectedIndex]
从数组中获取对象,并分配给
detailViewController
对象


单击单元格时,确保
segue
ViewController
ViewController
。它将调用
didSelectRow
。在
didSelectRow
中保存
indexPath.row
并使用标识符执行
performsguewithidentifier

现在在
prepareForSegue
中,使用
fines[selectedIndex]
从数组中获取对象,并分配给
detailViewController
对象


确保
segue
是从
ViewController
ViewController
正如Arun所指出的,您需要使用
-prepareforsgue
。以下是我将要做的事情,以便将某些内容传递给您的详细视图控制器(比如
deal.name

最后,只需使用
故事板将
UITableViewCell
MyDetailViewController
连接起来(并选择您喜欢的任何演示和转换样式)

为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
}

正如Arun所指出的,您需要使用
-prepareforsgue
。下面是我将要做的事情,以便将某些内容传递给您的详细视图控制器(比如
deal.name

最后,只需使用
故事板将
UITableViewCell
MyDetailViewController
连接起来(并选择您喜欢的任何演示和转换样式)

为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
}