如何将选中的数据从PopUpTableViewController传递到Swift中的其他TableViewContro



我有两个视图控制器:在第一个有TableView的ViewController中,我在每个TableViewCell中都有一个按钮来选择运输选项。单击按钮时,将弹出另一个TableViewController,其中包含运输选项列表。在选择运输选项后,我需要将此数据传递回第一个ViewController中的TableViewCell。我写了下面的代码,但在第二个TableViewController中选择的发货选项仍然没有传递给第一个控制器。其他方面都很好。有人能帮我知道如何改进这个代码吗?万分感谢!

//第一个视图控制器:

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
}
}

//第一个ViewController的TableViewCell:

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()   
}
}

//Second 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)

}
}

为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
}
}

使用ShippingOptions更新扩展发送

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
}
}

最新更新