Как передать выбранные данные из PopUpTableViewController в другой TableViewController в Swift? - PullRequest
0 голосов
/ 02 апреля 2020

У меня есть 2 контроллера представления: в первом ViewController, который имеет TableView, у меня есть кнопка в каждом TableViewCell, чтобы выбрать вариант доставки. При нажатии на кнопку появится другой TableViewController со списком параметров доставки. После выбора способа доставки мне нужно передать эти данные обратно в TableViewCell в первом ViewController. Я написал код ниже, но опция доставки, выбранная во втором TableViewController, все еще не прошла к первому контроллеру. Другие вещи работают нормально. Может ли кто-нибудь помочь сообщить мне, как улучшить этот код? Спасибо миллион!

// Первый ViewController:

class PaymentMethodViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    //MARK: - IBOutlets

    @IBOutlet weak var PurchasedReviewItemsTableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "PurchasedReviewItemsTableViewCell") as! PurchasedReviewItemsTableViewCell

        cell.delegate = self

    }

}

extension PaymentMethodViewController: PurchasedReviewItemsTableViewCellDelegate {

    func chooseShippingOptionButtonPressed() {

        let chooseShippingOptionVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "ShippingOptionsSelectionPopUpViewController") as! ShippingOptionsSelectionPopUpViewController

        chooseShippingOptionVC.modalPresentationStyle = .overCurrentContext

        self.present(chooseShippingOptionVC, animated: true, completion: nil)
    }

    //MARK: Pass data from popUpView

    func popUpShippingOptionsSelected(shippingOption: String) {

        let cell = PurchasedReviewItemsTableView.dequeueReusableCell(withIdentifier: "PurchasedReviewItemsTableViewCell") as! PurchasedReviewItemsTableViewCell
        cell.shippingOptionsLabel.text = shippingOption

    }

}

// TableViewCell первого ViewController:

protocol PurchasedReviewItemsTableViewCellDelegate {

    func chooseShippingOptionButtonPressed()

    func popUpShippingOptionsSelected(shippingOption: String)
}


class PurchasedReviewItemsTableViewCell: UITableViewCell {

    @IBOutlet weak var shippingOptionsLabel: UILabel!

    var delegate: PurchasedReviewItemsTableViewCellDelegate?
    @IBAction func changeShippingOptionButtonPressed(_ sender: Any) {

        delegate?.chooseShippingOptionButtonPressed()   
    }

}

// Второй TableViewController:

class ShippingOptionsSelectionPopUpViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var selectedShippingOption : String?

    var shippingOption = ["X", "Y"]

    var delegate: ShippingOptionsSelectionPopUpDelegate?

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ShippingOptionsSelectionPopUpTableViewCell", for: indexPath) as! ShippingOptionsSelectionPopUpTableViewCell

        cell.selectShippingOption(shippingOption: shippingOption[indexPath.row])

        return cell
    }

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

        selectedShippingOption = shippingOption[indexPath.row]

        delegate?.popUpShippingOptionsSelected(shippingOption: selectedShippingOption!)

        dismiss(animated: true, completion: nil)


    }
}

1 Ответ

0 голосов
/ 02 апреля 2020

Определить отдельный протокол для popUpShippingOptionsSelected

protocol ShippingOptionsDelegate {

func popUpShippingOptionsSelected(shippingOption: String)
}

class ShippingOptionsSelectionPopUpViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var selectedShippingOption : String?

var shippingOption = ["X", "Y"]

var shippingOptiondelegate: ShippingOptionsSelectionPopUpDelegate?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            2
        }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "ShippingOptionsSelectionPopUpTableViewCell", for: indexPath) as! ShippingOptionsSelectionPopUpTableViewCell

        cell.selectShippingOption(shippingOption: shippingOption[indexPath.row])

        return cell
    }

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

        selectedShippingOption = shippingOption[indexPath.row]

        shippingOptiondelegate?.popUpShippingOptionsSelected(shippingOption: selectedShippingOption!)

        dismiss(animated: true, completion: nil)


    }
}

Добавить тег в ячейку

class PaymentMethodViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

//MARK: - IBOutlets

@IBOutlet weak var PurchasedReviewItemsTableView: UITableView!

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "PurchasedReviewItemsTableViewCell") as! PurchasedReviewItemsTableViewCell

cell.delegate = self
cell.tag = 100 // Set tag

}

}

Обновить расширение с ShippingOptionsDelegate

extension PaymentMethodViewController: PurchasedReviewItemsTableViewCellDelegate, ShippingOptionsDelegate  {

func chooseShippingOptionButtonPressed() {

            let chooseShippingOptionVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "ShippingOptionsSelectionPopUpViewController") as! ShippingOptionsSelectionPopUpViewController
             chooseShippingOptionVC.shippingOptiondelegate = self
             chooseShippingOptionVC.modalPresentationStyle = .overCurrentContext

             self.present(chooseShippingOptionVC, animated: true, completion: nil)
}

//MARK: Pass data from popUpView

func popUpShippingOptionsSelected(shippingOption: String) {

        let cell = PurchasedReviewItemsTableView.viewWithTag(100) as! PurchasedReviewItemsTableViewCell // use tag to get cell
        cell.shippingOptionsLabel.text = shippingOption

}

}
...