首页 > 解决方案 > 将选择的值传递到表格视图单元格内的标签中

问题描述

我对 swift 比较陌生,但我仍在努力学习一些基础知识。但是,谢谢大家在这方面帮助我。Ok when a UITableViewCellis selected a PopUpViewControlleris presented with a datepicker so the user can select a desire date and when dissmisButtonis pressed the current view is dismissed and as soon that happens I want the selected value to show on the label embedded on the selected UITableViewCell. 代码开启PopUpViewController

import UIKit

class PopUpColchonesViewController: UIViewController {

    var tipoColchonArray = [String](arrayLiteral: "Individual", "Matrimonial", "King Size")

    @IBOutlet weak var tipoColchonLb: UILabel!
    @IBOutlet weak var tipoColchonPicker: UIPickerView!
    @IBOutlet weak var guardarTipoColchon: UIButton!
    var tipoSeleccionado : String?

    override func viewDidLoad() {
        super.viewDidLoad()
        tipoColchonPicker.delegate = self
        tipoColchonPicker.dataSource = self

        // Do any additional setup after loading the view.
    }

    @IBAction func guardarTipo(_ sender: Any) {
        NotificationCenter.default.post(name: .guardarTipo, object: nil)
        dismiss(animated: true, completion: nil)
    }


}

extension PopUpColchonesViewController : UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return tipoColchonArray.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return tipoColchonArray[row]
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let selectedItem = tipoColchonArray[row]
        tipoSeleccionado = selectedItem

    }
}

extension Notification.Name {
    static let guardarTipo = Notification.Name(rawValue: "guardarTipo")
}

到目前为止我尝试过的

  var observer : NSObjectProtocol?

    var cellServicio = UITableViewCell()

    override func viewDidLoad() {
        super.viewDidLoad()
        servicioHogarTB1.delegate = self
        servicioHogarTB1.dataSource = self
        servicioHogarTB1.register(UINib(nibName: "ServicioCell", bundle: nil), forCellReuseIdentifier: "servicioCell")
        servicioHogarTB1.separatorStyle = .none

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        NotificationCenter.default.addObserver(self, selector: #selector(handlePopupClosing), name: .guardarTipo, object: nil)
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        if let observer = observer {
            NotificationCenter.default.removeObserver(observer)
        }
        print(servicioSeleccionado)
    }

    @objc func handlePopupClosing (notification : NSNotification) {
        let tipoVC = notification.object as? PopUpColchonesViewController
        let index = servicioHogarTB1.indexPathForSelectedRow
        if let cell = servicioHogarTB1.cellForRow(at: index!) as? ServicioCell {
            cell.servicioTipoLb.text = tipoVC?.tipoSeleccionado
        }
    }

到目前为止的结果是在展开可选值时出现错误,并且未显示标签上的文本。

标签: swiftuitableviewnsnotificationcenter

解决方案


像这样改变你的方法并检查......希望它会起作用

 @IBAction func guardarTipo(_ sender: Any) {

        dismiss(animated: true, completion: {
            NotificationCenter.default.post(name: .guardarTipo, object: nil)
        })
    }

推荐阅读