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.
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)
other viewController:
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() 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() {
...}