简体   繁体   English

快速在 tableview 和 viewController 之间传递值

[英]swift pass the values between tableview and viewController

I know this question is being asked lot of times..trying from last 2 days.我知道这个问题被问了很多次......从过去 2 天开始尝试。 Please help me.请帮我。

I have a custom TableView trying pass few data from Array of this Tableview to next View.我有一个自定义的 TableView,试图将一些数据从这个 Tableview 的 Array 传递到下一个 View。

Error : unexpectedly found nil while unwrapping an Optional value.错误:在解开 Optional 值时意外发现 nil。

I tried many ways.. but no success.我尝试了很多方法......但没有成功。

EDITED已编辑

Finally Solved终于解决了

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


    if segue.identifier == "SegueToQR"
    {
        let controller = segue.destinationViewController as! QRcodeViewController
        if let button = sender as? UIButton {
            let cell = button.superview?.superview?.superview as! PatientTableViewCell
            controller.passName = cell.patientName.text
            controller.passAdress = cell.adress.text
        }
    }

    }

Your logic is wrong.你的逻辑是错误的。 The UITableView only maintains cells that are visible on the screen. UITableView 只维护在屏幕上可见的单元格。 So if you try to access a cell that is not visible, it will give nil exception.因此,如果您尝试访问不可见的单元格,它将给出 nil 异常。

First do not use CGPoint to get the index.首先不要使用CGPoint来获取索引。 Assign a tag to each button in your cell, and pass that button in the sender parameter of the performSegue function.为单元格中的每个按钮分配一个标签,并在 performSegue 函数的 sender 参数中传递该按钮。

Then you should maintain your patient data in an array.然后你应该在一个数组中维护你的病人数据。 And then access the data using the button.tag as index to the array.然后使用 button.tag 作为数组的索引访问数据。 You will get your patient model object, from this object you can pass the data to the next view controller.您将获得患者模型对象,您可以从该对象将数据传递给下一个视图控制器。

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

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