【问题标题】:Unable of passing data from UIPickerView to CustomTableViewCell无法将数据从 UIPickerView 传递到 CustomTableViewCell
【发布时间】:2019-04-13 20:02:56
【问题描述】:

我正在尝试一些相对简单的事情,但我无法解决这个问题。我有一个嵌入了 UITableView 的注册视图控制器。 TableView 有 CustomsCells 已经嵌入了三种自定义单元格。 The last one is customize with a UITextfield and when selected is suppose to trigger from the bottom the PickerView so the user can select form there the desire preference.除了一件小事,一切都很好;选择任何行时,文本应该是TextField的输入,但这没有发生。

我在互联网上尝试了几段代码,但大多数都是关于文本字段在同一个 ViewController 中的,因为在这种情况下 UITextFieldDelegate 字段连接到 de CustomTableViewCell 类。我曾尝试创建此 CustomTableViewCell 的对象,然后只需点击 textfield 变量并尝试更改 text 属性,但结果只是空白。

import UIKit

class RegisterViewController: UIViewController {

    let registrationCell = RegisterViewCell()
    let personalCell = PersonalViewCell()
    var selectedRow : String = ""

    let registerArray = ["UserName"]
    let passwordArray = ["Password", "Confirm Password"]
    let personalArray = ["I am"] //Identity of the user, female or male
    let personalPickerArray = ["Famale", "Male"]
    @IBOutlet weak var registerTableView: UITableView!
    @IBOutlet weak var personalPickerView: UIPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        personalPickerView.isHidden = true
        personalCell.personalTextField?.inputView = personalPickerView

    } 

我为 TableView 和 Picker View 创建了扩展,我认为问题出在此处,但我不确定:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let personalCell = tableView.dequeueReusableCell(withIdentifier: "PersonalViewCell", for: indexPath) as! PersonalViewCell
            personalCell.personalLabel?.text = personalArray[0]
            personalCell.personalTextField.text = selectedRow
            personalCell.accessoryType = .disclosureIndicator
            return personalCell
        }

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

        if indexPath.row == 3 {
            personalPickerView.isHidden = false
        }
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        let row = personalPickerArray[row]
        selectedRow = row
        personalPickerView.isHidden = true
        print(selectedRow)
    }

自定义单元类文件

import UIKit

class PersonalViewCell: UITableViewCell {


    @IBOutlet weak var personalLabel: UILabel!
    @IBOutlet weak var personalTextField: UITextField!


    override func awakeFromNib() {
        super.awakeFromNib()
    }
}

Right now when a row is selected im expecting to have an input in the UITextField embedded inside the Custom Cell "Personal View Cell" but when any row is selected the UITextField is just blank

【问题讨论】:

  • 您是想将选择器值放入文本字​​段还是将文本字段值放入选择器?
  • 您好,感谢您的检查,我正在尝试将选择器值放入文本字​​段。
  • You can store the indexPath of the text field row, and when the picker delegate is called use the method tableView.cellForRow(at: indexPath) to get the text field cell and then update the text field with your选择器值。

标签: swift uitableview uitextfield


【解决方案1】:
class RegisterViewController: UIViewController {

    var selectedIndexPath:IndexPath?
    /* code for class  */


  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var selectedIndexPath = indexPath //store index path
        if indexPath.row == 3 {
            personalPickerView.isHidden = false
        }
    }


 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        let row = personalPickerArray[row]
        selectedRow = row
        personalPickerView.isHidden = true

       /* check if we have an index path, and if the cell is correct type */
        if let indexPath = selectedIndexPath {
            if let cell = tableView.cellForRow(at: indexPath) as? PersonalViewCell {
                   cell.textField.text = personalPickerArray[row]
             }
        }

        print(selectedRow)
    }

} 

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多