从 UITableViewCell 中的 UITextField 获取数据 [英] obtain data from UITextField in UITableViewCell

查看:21
本文介绍了从 UITableViewCell 中的 UITextField 获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have crated this tableview to obtain the users children's names and store them into an array called dependentsArray:

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

I have searched through stackoverflow, Apple documentation, google, and youtube tutorials, and cant seem to find a solution. This seems like it should be simple but its causing quite the headache. The closest i found to a solution was this post:

从每个 UITableView Cells Swift 中获取数据

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

However, i am not obtaining the cells textfield value by tapping them, so i cannot use didDeselectRowAtIndexPath. I wish to obtain the text values when i tap the "Next" button.

tableview 根据一个名为 dependentsCount 的整数扩展和收缩其单元格计数.我增加和减少 dependentsCount 像这样:

The tableview expands and contracts its cell count based on an integer called dependentsCount. I increment and decrement the dependentsCount like so:

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

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

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

The ways in which ive tried to obtain the value of text fields are as follows:

    @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)

}

导致应用程序崩溃 ^^

which crashed the app ^^

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

什么都不做^^

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

推荐答案

我建议你试试我在 这个要点给你(来源是交互式游乐场a>).

I would suggest you to try something like I wrote in this gist for you (the source is for interactive playground).

简而言之,我建议为您的数据引入模型:

Briefly, I suggest introducing a model for your data:

class Model {

    let placeholder: String?
    var text: String?

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

}

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

Then making a list of models to actually store the data:

// 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")
]

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

And connecting visible cells to the corresponding models via an appropriate data source.

单元格可能是这样的:

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

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

After that, you can read the data entered by user at any time from the list of the models.

在交互式 iOS 游乐场中玩要点

Play with the gist in an interactive iOS playground!

这篇关于从 UITableViewCell 中的 UITextField 获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆