【问题标题】:obtain data from UITextField in UITableViewCell从 UITableViewCell 中的 UITextField 获取数据
【发布时间】:2016-04-18 14:49:03
【问题描述】:

我已经创建了这个 tableview 来获取用户孩子的名字并将它们存储到一个名为 dependentsArray 的数组中:

我搜索了 stackoverflow、Apple 文档、google 和 youtube 教程,但似乎找不到解决方案。这似乎应该很简单,但它引起了相当的头痛。我发现最接近解决方案的是这篇文章:

getting data from each UITableView Cells Swift

但是,我没有通过点击单元格文本字段值来获取它们,所以我不能使用didDeselectRowAtIndexPath。我希望在点击“下一步”按钮时获取文本值。

tableview 根据名为dependentsCount 的整数扩展和收缩其单元格计数。我像这样递增和递减dependentsCount

    @IBAction func subtractDependentButtonClick(sender: AnyObject) {
    dependentsCount -= 1
    dependentsTableView.reloadData()
    fadeTransition(0.3)
}

@IBAction func addDependentButtonClick(sender: AnyObject) {
    dependentsCount += 1
    dependentsTableView.reloadData()
    fadeTransition(0.3)
}

我尝试获取文本字段值的方式如下:

    @IBAction func nextButtonPress(sender: AnyObject) {
    var index = 0
    var cell = self.dependentsTableView.cellForRowAtIndexPath(NSIndexPath(index: index)) as! DependentsTableViewCell

    while index < dependentsCount
    {
        self.dependentsArray.append(cell.nameTextField.text!)
        index += 1
    }
    self.performSegueWithIdentifier("showNextVC", sender: nil)

}

导致应用程序崩溃^^

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    let cell = dependentsTableView.dequeueReusableCellWithIdentifier("Cell") as! DependentsTableViewCell
    dependentsArray.append(cell.nameTextField.text!)
    print("\(dependentsArray)")
}

什么都不做^^

获取tableview单元格文本字段文本的正确方法是什么?

【问题讨论】:

  • 你在UITableView中为DependentsTableViewCell注册了nib还是类?
  • 是的,我是在属性检查器中完成的
  • 如果您使用笔尖,请确保插座已连接。这听起来像这里的问题。
  • 我在 'DependentsTableView' 类文件中有一个文本字段的出口
  • 如果我没记错的话,在笔尖中,第一个顶级对象应该是表格视图单元格子类,而不是文件的所有者。确保是这种情况。

标签: ios swift uitableview uitextfield


【解决方案1】:

我建议你试试我在this gist 中为你写的东西(来源是interactive playground)。

简单地说,我建议为您的数据引入模型

class Model {

    let placeholder: String?
    var text: String?

    init(placeholder: String?, text: String? = nil) {
        self.placeholder = placeholder
        self.text = text
    }

}

然后制作一个模型列表来实际存储数据:

// Sample items.
let items = [
    TextFieldCell.Model(placeholder: "1"),
    TextFieldCell.Model(placeholder: "2"),
    TextFieldCell.Model(placeholder: "3", text: "xxx"),
    TextFieldCell.Model(placeholder: "4"),
    TextFieldCell.Model(placeholder: "5"),
    TextFieldCell.Model(placeholder: "6")
]

并通过适当的数据源将可见单元格连接到相应的模型。

单元格可能是这样的:

class TextFieldCell: UITableViewCell, SmartCell, UITextFieldDelegate {

    var model: Model? {
        didSet {
            textField.placeholder = model?.placeholder
            textField.text = model?.text
        }
    }

    let textField = UITextField()

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        commonInit()
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        commonInit()
    }

    private func commonInit() {
        contentView.addSubview(textField)
        textField.delegate = self
    }

    override func layoutSubviews() {
        textField.frame = contentView.bounds.insetBy(dx: 15, dy: 8)
    }

    func loadModel(m: Model) {
        model = m
    }

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        let text = (textField.text as NSString?)?.stringByReplacingCharactersInRange(range, withString: string)
        // Update the model.
        model?.text = text
        return true
    }
}

之后,您可以随时从模型列表中读取用户输入的数据。

在交互式 iOS 游乐场中与 the gist 一起玩!

【讨论】:

    【解决方案2】:

    更新的答案,新的 didDeselectRowAtIndexPath

    func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    if let textView = self.view.viewWithTag(indexPath.row) as? UITextField
    {
        dependentsArray.append(textView.text!)
    }
    print("\(dependentsArray)")
    }
    

    并将其放入 cellForRowAtIndexPath

    cell.nameTextField.tag = indexPath.row
    

    【讨论】:

    • 没有抛出任何错误,也没有打印出任何东西。你的意思是“didSelectRowAtIndexPath”还是“didDeselectRowAtIndexPath”?
    • 此方法不适用于屏幕外单元格。此外,屏幕外单元格中的数据也会丢失。
    • @werediver 是对的,我将其更改为我原来的答案,如果它没有打印任何内容,请确保调用 didDeselectRowAtIndexPath...
    猜你喜欢
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2011-08-09
    • 2012-03-23
    • 2011-09-19
    相关资源
    最近更新 更多