![](/img/trans.png)
[英]Swift 3: retrieving text from a UITextField in a custom UITableViewCell
[英]Swift: retrieving text from a UITextField in a custom UITableViewCell and putting it in an array
我正在制作一个非常简单的应用程序,用户在其中输入第一个屏幕中的人数。
在第二个屏幕中,它根据用户在第一个屏幕中输入的数字生成多个UITableViewCell
。 UITableViewCell
有一个UITextField
,一旦用户点击进入第三个屏幕,我就会尝试将在这些字段中输入的数据存储在一个数组中。
我怎样才能做到这一点?
提前致谢!
以下是为我的UIViewController
调用自定义UITableViewCell
的代码:
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 的内容将丢失。 如果您可以确定这永远不会发生,请使用以下代码(在处理按钮点击的方法中)来组成您的数组:
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
文本字段设置delegate
,然后使用委托方法获取输入的名称。
首先,将变量添加到您的视图控制器,以保存当前正在编辑的文本字段的数组和行号。
var arrayOfNames : [String] = [String]()
var rowBeingEdited : Int? = nil
然后,在您的cellForRowAtIndexPath
方法中,添加:
cell.nameInput.text = "" // just in case cells are re-used, this clears the old value
cell.nameInput.tag = indexPath.row
cell.nameInput.delegate = self
然后添加两个新函数,以捕捉文本字段何时开始/结束编辑:
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
}
当用户点击按钮时,他们可能仍在编辑名称之一。 为此,在处理按钮点击的方法中添加以下内容:
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
方法,从而将文本保存到数组中。
这里有新的快速版本的答案
var arrayOfNames : [String] = [String]()
var i = 0
while i < taskArrForRead.count {
let indexPath = IndexPath(row: i, section: 0)
let cell : taslakDuzenlemeCell? = self.tableView.cellForRow(at: indexPath) as! taslakDuzenlemeCell?
if let item = cell?.taslakTextField.text {
arrayOfNames.append(item)
}
i = i + 1
}
print("\(arrayOfNames)")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.