当我在 swift4 中单击 tableviewcell 时如何传递数据? [英] how to pass data when i click the tableviewcell in swift4?

查看:35
本文介绍了当我在 swift4 中单击 tableviewcell 时如何传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据从 tableview 发送到另一个 ViewController 的 tableview.但是,在传递数据时,数据被转换为可选类型.

I want to send data from the tableview to the tableview of another ViewController. However, when pass the data, the data is converted to optional type.

所以在另一个ViewController中要执行的SQL语句没有被执行.ViewDidLoad 中出现可选错误.

So the SQL statement to be executed in another ViewController is not executed. An optional error occurred in ViewDidLoad.

我也使用了覆盖准备功能,但结果是一样的.我不明白为什么会这样.

I have also used the override prepare function, but the result is the same. I can't understand why this is happening.

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let itemCd = self.itemList[indexPath.row].itemCd
    let itemName = self.itemList[indexPath.row].itemName

    print(self.itemList[indexPath.row].itemCd)

    let infoVC = self.storyboard?.instantiateViewController(withIdentifier: "HIST_RVC")
    if let _infoVC = infoVC as? HistviewController {

        _infoVC.itemCd = Int(itemCd)
        _infoVC.itemName = String(itemName)

        print(_infoVC.itemCd)
        self.performSegue(withIdentifier: "moveHist", sender: self)
    }

当我打印时,我得到以下结果,

When I print, I get the following result,

first : 1,
second : Optional(1)

其他视图控制器:

class HistviewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

var itemCd: Int!
var itemName: String!
// SQLite DB
var infomationDAO = infoDAO()
var infoList: [InfoVO]!

override func viewDidLoad() {

    self.infoList = self.infomationDAO.find(itemCd: self.itemCd)
    self.tableview.reloadData()
}

sql find() 函数:

sql find() function:

func find(itemCd: Int = 0) -> [InfoVO] {

    var infoList = [InfoVO]()

    do {
        // define condition
        let condition = itemCd == 0 ? "": "WHERE a.item_cd = \(itemCd)"

        let sql = """
        select a.info_cd, a.info_name, b.item_name
        from info_table a
        join item_table b
        on a.info_name = b.item_name
        \(condition)
        order by a.item_cd ASC
        """

        let rs = try self.fmdb.executeQuery(sql, values: nil)

        while rs.next() {
        ...}

推荐答案

根据你的代码你正在执行 segue 所以你需要实现 func prepare(for segue: UIStoryboardSegue, sender: Any?)您的控制器并传递值.

As per your code your performing segue so you need to implement func prepare(for segue: UIStoryboardSegue, sender: Any?) in your controller and pass the value.

代码:

 var selectedIndex = 0
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let controller = segue.destination as? HistviewController {
                let itemCd = self.itemList[selectedIndex].itemCd
                let itemName = self.itemList[selectedIndex].itemName
                controller.itemcd = Int(itemcd)
                controller.itemName = String(itemName)
            }
        }

        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            self.selectedIndex = indexPath.row
        }

这篇关于当我在 swift4 中单击 tableviewcell 时如何传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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