swift - 将选择的值传递到表格视图单元格内的标签中
问题描述
我对 swift 比较陌生,但我仍在努力学习一些基础知识。但是,谢谢大家在这方面帮助我。Ok when a UITableViewCell
is selected a PopUpViewController
is presented with a datepicker so the user can select a desire date and when dissmisButton
is 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
}
}
到目前为止的结果是在展开可选值时出现错误,并且未显示标签上的文本。
解决方案
像这样改变你的方法并检查......希望它会起作用
@IBAction func guardarTipo(_ sender: Any) {
dismiss(animated: true, completion: {
NotificationCenter.default.post(name: .guardarTipo, object: nil)
})
}
推荐阅读
- javascript - 如何正确使用 document.querySelectorAll()?
- javascript - 打印时需要隐藏未选中的框
- c# - C# 中索引的替代方案(在 Swift 中)
- reactjs - 我应该用什么代替路由器中的 onEnter 和 onChange?
- sql-server - 将 HH:MM TT 转换为时间并附加到日期
- python - 列出两个列表之间的所有组合,其中第一个列表的元素小于第二个列表的元素
- xml - 需要使用 SQL 查询中的值更新 XML 字符串中的多个节点
- database - org.h2.jdbc.JdbcSQLException:十六进制字符串包含非十六进制字符:
- javascript - 如何在 Vue.js 中获取完整的当前日期和时间,而无需编写大量 JavaScript 行
- github - 无法从 github 存储库中删除可疑文件