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

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

问题描述

我正在制作一个非常简单的应用,用户可以在其中输入第一个屏幕中的人数.

在第二个屏幕中,它根据用户在第一个屏幕中输入的数字生成多个 UITableViewCell.UITableViewCell 在其中有一个 UITextField,一旦用户点击进入第三个屏幕,我就会尝试将在这些字段中输入的数据存储在一个数组中.>

我该怎么做?
提前致谢!

我正在使用故事板.

以下是我的 UIViewController 调用自定义 UITableViewCell 的代码:

func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->UITableViewCell{var cell: EditingCell = tableView.dequeueReusableCellWithIdentifier("Cell") as EditingCell如果 indexPath.row % 2 == 0{cell.backgroundColor = UIColor.purpleColor()} 别的 {cell.backgroundColor = UIColor.orangeColor()}let person = arrayOfPeople[indexPath.row]cell.setCell(person.name)返回单元格}

UITableViewCell 的代码如下所示:

class EditingCell: UITableViewCell{@IBOutlet 弱变量名称输入:UITextField!覆盖 funcawakeFromNib() {super.awakeFromNib()//初始化代码}覆盖 func setSelected(selected: Bool, 动画: Bool) {super.setSelected(选中,动画:动画)//为选中状态配置视图}func setCell(名称:字符串){self.nameInput.placeholder = 名字}}

解决方案

如果表格中的行数超过了屏幕所能容纳的行数,那么您的方法就会出现问题.在这种情况下,滚动到屏幕外的单元格将被重新使用,并且 nameInput textField 的内容将丢失.如果您可以确定这永远不会发生,请使用以下代码(在处理按钮点击的方法中)来组成您的数组:

 var arrayOfNames : [String] = [String]()对于 var i = 0;i

或者……

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

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

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

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

 cell.nameInput.text = ""//以防重复使用单元格,这会清除旧值cell.nameInput.tag = indexPath.rowcell.nameInput.delegate = self

然后添加两个新函数,以捕捉文本字段何时开始/结束

func textFieldDidEndEditing(textField: UITextField) {让行 = textField.tag如果行 >= arrayOfNames.count {对于 var addRow = arrayOfNames.count;addRow <= 行;添加行++ {arrayOfNames.append("")//这会添加空白行以防用户跳过行}}arrayOfNames[row] = textField.textrowBeingEdited = nil}func textFieldDidBeginEditing(textField: UITextField) {rowBeingEdited = textField.tag}

当用户点击按钮时,他们可能仍在编辑其中一个名称.为此,在处理按钮点击的方法中添加以下内容:

 if let row = rowBeingEdited {让 indexPath = NSIndexPath(forRow:row, inSection:0)让单元格:EditingTableViewCell?= self.tableView.cellForRowAtIndexPath(indexPath) 作为 EditingTableViewCell?cell?.nameTextField.resignFirstResponder()}

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

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

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!

Edit: I'm using the storyboard.

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
}

Here is what the code for the UITableViewCell looks like:

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

解决方案

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

Alternatively....

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

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

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天全站免登陆