如何将数据从UITableView传递到UIViewController而不进行隔离

时间:2019-06-16 10:33:03

标签: swift uitableview uiviewcontroller

我需要将UITableView中的行标题传递给我的ViewController。我在情节提要中没有VC(我已经通过代码创建了它)。所以我不能使用segue。

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

   let cell = tableView.cellForRow(at: indexPath) as! UITableViewCell


    cell.textLabel?.textColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
    cell.textLabel?.tintColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)

        var titleOfFood = String()
        if searching == true {
           titleOfFood = searchedFoods[indexPath.row].title
            cell.textLabel?.textColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
             print(titleOfFood)
        } else {
            titleOfFood = foods[indexPath.row].title
            cell.textLabel?.textColor = UIColor(red: 0, green: 122/255, blue: 1, alpha: 1)
             print(titleOfFood)
        }

    let controller = EatenFoodViewController()
    let transitionDelegate = SPStorkTransitioningDelegate()
    controller.transitioningDelegate = transitionDelegate
    controller.modalPresentationStyle = .custom
    //controller.delegate = self
    transitionDelegate.customHeight = 620
    transitionDelegate.showIndicator = false
    self.present(controller, animated: true, completion: nil)
}

我需要将titleOfFood传递给EatenFoodViewController

1 个答案:

答案 0 :(得分:1)

您无需先将数据发送到目标vc即可,只需执行

controller.titleOfFood = arr[indexPath.row].title // assuming you have an array of models 

OR

controller.titleOfFood = cell.textLabel!.text!

class EatenFoodViewController:UIViewController {
   var titleOfFood = ""
}