将segue从UITableViewCell推送到Swift中的ViewController [英] Push segue from UITableViewCell to ViewController in Swift

查看:110
本文介绍了将segue从UITableViewCell推送到Swift中的ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了UITableViewCells的问题。我将我的UITableView连接到API以填充我的单元格。

I'm encountering problems with my UITableViewCells. I connected my UITableView to a API to populate my cells.

然后我创建了一个抓取 indexPath.row 的函数来识别数组中的哪个JSON对象应该发送到 RestaurantViewController

Then I've created a function which grabs the indexPath.row to identify which JSON-object inside the array that should be sent to the RestaurantViewController.


链接到我的Xcode项目以便于调试和解决问题

以下是我的小片段查找设置的方式全局变量的row-clicks。

Here's how my small snippet looks for setting the "row-clicks" to a global variable.

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     i = indexPath.row
}

这是我的 prepareForSegue ()应该将我的push-segue连接到 RestaurantViewController 的函数。

And here's my prepareForSegue() function that should hook up my push-segue to the RestaurantViewController.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "toRestaurant"{
    let navigationController = segue.destinationViewController as UINavigationController
    let vc = navigationController.topViewController as RestaurantViewController
    vc.data = currentResponse[i] as NSArray
 }
}

这是怎么回事我已经从 UITableViewCell

And here's how I've set up my segue from the UITableViewCell

这是我的结果,我试图点击这些单元格中的每一个但我不会被推到另一个viewController ...我也不喜欢没有错。这里有什么问题?

Here's my result, I've tried to click every single one of these cells but I won't be pushed to another viewController...I also don't get an error. What is wrong here?


尝试不起作用的解决方案

Tried solutions that won't work



override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
        if segue.identifier == "toRestaurant"{
            let vc = segue.destinationViewController as RestaurantViewController
            //let vc = navigationController.topViewController as RestaurantViewController
            vc.data = currentResponse[i] as NSArray
        }
    }


推荐答案

问题是您没有正确处理数据。
如果您查看 currentResponse 数组,您会看到它包含NSDictionaries,但在 prepareForSegue 中您尝试将NSDictionary强制转换为NSArray,这将使应用程序崩溃。

The problem is that you're not handling your data correctly. If you look into your currentResponse Array, you'll see that it holds NSDictionaries but in your prepareForSegue you try to cast a NSDictionary to a NSArray, which will make the app crash.

更改数据变量 RestaurantViewController 到NSDictionary并更改 prepareForSegue 以传递NSDictionary

Change the data variable in RestaurantViewController to a NSDictionary and change your prepareForSegue to pass a a NSDictionary

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if let cell = sender as? UITableViewCell {
        let i = redditListTableView.indexPathForCell(cell)!.row
        if segue.identifier == "toRestaurant" {
            let vc = segue.destinationViewController as RestaurantViewController
            vc.data = currentResponse[i] as NSDictionary
        }
    }
}

这篇关于将segue从UITableViewCell推送到Swift中的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆