Swift:从自定义UITableViewCell中的UITextField中检索文本并将其放入数组中 [英] Swift: retrieving text from a UITextField in a custom UITableViewCell and putting it in an array

查看:374
本文介绍了Swift:从自定义UITableViewCell中的UITextField中检索文本并将其放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个非常简单的应用,用户在第一个屏幕中输入人数。

I'm making a very simple app where the user enters the number of people in the first Screen.

在第二个屏幕中,它会生成一个 UITableViewCell 根据用户在第一个屏幕中输入的数字。 UITableViewCell 中有一个 UITextField ,我试图将这些字段中输入的数据存储在一个数组中用户单击以转到第三个屏幕。

In the second screen it generates a number of UITableViewCell based on the number the user entered in the first screen. The UITableViewCell have a UITextField in them and I'm trying to store the data entered in those fields in an array once the user clicks to go to the third screen.

我该怎么做?
提前致谢!

How can I do that?
Thanks in advance!

这是什么调用自定义 UITableViewCell 的代码看起来像我的 UIViewController

Here is what the code that calls for the custom UITableViewCell looks like for my UIViewController:

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    var cell: EditingCell = tableView.dequeueReusableCellWithIdentifier("Cell") as EditingCell

    if indexPath.row % 2 == 0{
        cell.backgroundColor = UIColor.purpleColor()
    } else {
        cell.backgroundColor = UIColor.orangeColor()
    }

    let person = arrayOfPeople[indexPath.row]

    cell.setCell(person.name)


    return cell
}

以下是 UITableViewCell 的代码:

class EditingCell: UITableViewCell{

    @IBOutlet weak var nameInput: UITextField!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }


    func setCell(name:String){
        self.nameInput.placeholder = name
    }
}


推荐答案

如果表格中的行数超出了屏幕上可以容纳的数量,那么您的方法会出现问题。在这种情况下,将重新使用滚出屏幕的单元格,并且 nameInput textField的内容将丢失。如果您可以确定这种情况永远不会发生,请使用以下代码(在处理按钮的方法中)来编写数组:

There is a problem with your approach if the number of rows in your table exceeds the number that can fit on screen. In that case, the cells that scroll off-screen will be re-used, and the contents of the nameInput textField will be lost. If you can be sure that this will never happen, use the following code (in the method that handles button taps) to compose your array:

        var arrayOfNames : [String] = [String]()
        for var i = 0; i<self.arrayOfPeople.count; i++ {
            let indexPath = NSIndexPath(forRow:i, inSection:0)
            let cell : EditingCell? = self.tableView.cellForRowAtIndexPath(indexPath) as EditingCell?
            if let item = cell?.nameInput.text {
                arrayOfNames.append(item)
            }
        }
        println("\(arrayOfNames)")

另选....

但是,如果单元格可能会在屏幕外滚动,我建议采用不同的解决方案。为 nameInput 文本字段设置委托,然后使用委托方法获取输入的名称。

However, if it is possible that cells will scroll off-screen, I suggest a different solution. Set the delegate for the nameInput text fields, and then use the delegate methods to grab the names as they are entered.

首先,将变量添加到视图控制器,以保存数组和当前正在编辑的文本字段的行号。

First, add variables to your view controller, to hold the array and the row number of the text field currently being edited.

    var arrayOfNames : [String] = [String]()
    var rowBeingEdited : Int? = nil

然后,在 cellForRowAtIndexPath 方法中,添加:

Then, in your cellForRowAtIndexPath method, add:

    cell.nameInput.text = "" // just in case cells are re-used, this clears the old value
    cell.nameInput.tag = indexPath.row
    cell.nameInput.delegate = self

然后添加两个新函数,以捕获文本字段开始/结束编辑的时间:

Then add two new functions, to catch when the text fields begin/end editing:

func textFieldDidEndEditing(textField: UITextField) {
    let row = textField.tag
    if row >= arrayOfNames.count {
        for var addRow = arrayOfNames.count; addRow <= row; addRow++ {
            arrayOfNames.append("") // this adds blank rows in case the user skips rows
        }
    }
    arrayOfNames[row] = textField.text
    rowBeingEdited = nil
}

func textFieldDidBeginEditing(textField: UITextField) {
    rowBeingEdited = textField.tag
}

当用户点击按钮时,他们可能仍在编辑其中一个名称。为了满足这个要求,请将以下内容添加到处理按钮水龙头的方法中:

When the user taps the button, they might still be editing one of the names. To cater for this, add the following to the method that handles the button taps:

        if let row = rowBeingEdited {
            let indexPath = NSIndexPath(forRow:row, inSection:0)
            let cell : EditingTableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as EditingTableViewCell?
            cell?.nameTextField.resignFirstResponder()
        }

这会强制textField完成编辑,从而触发 didEndEditing 方法,从而将文本保存到数组中。

This forces the textField to complete editing, and hence trigger the didEndEditing method, thereby saving the text to the array.

这篇关于Swift:从自定义UITableViewCell中的UITextField中检索文本并将其放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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