正如标题所述;我想从每个单元的多个UITextField
中读取数据并将它们存储在数组中。我该怎么办?
我创建了一个子类CustomCell
,其中有2个UITextFields
。 P / S:单元格的标识符也是CustomCell
非常感谢
class TableViewController : UITableViewController, UITextFieldDelegate {
var data = [input]()
@IBOutlet var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell") as! CustomCell
customCell.input1.tag = indexPath.row
customCell.input2.tag = indexPath.row
customCell.input1.delegate = self
customCell.input2.delegate = self
return customCell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
@IBAction func addButton(_ sender: Any) {
table.beginUpdates()
table.insertRows(at: [IndexPath(row: data.count-1, section: 0)], with: .automatic)
table.endUpdates()
}
func textFieldDidEndEditing(_ textField: UITextField) {
let indexPath = IndexPath(row: textField.tag, section: 0)
if let customCell = self.table.cellForRow(at: indexPath) as? CustomCell{
let a = customCell.Credit.text!.isEmpty ? no:String(customCell.input1.text!)
let b = customCell.letterGrade.text!.isEmpty ? no:String(customCell.input2.text!))
inputRead.append(input(string1: a, string2: b))
}
@IBAction func foo(_ sender: Any) {
if inputRead.count == 0{
return
}
//the rest of implementation
}
CustomCell类:
Import UIKit
public class CustomCell: UITableViewCell {
@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
}
答案 0 :(得分:1)
按照以下说明在视图控制器中更新代码。
在cellForRowAt
中将文本字段的委托自身分配给ViewController
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
customCell.input1.delegate = self
customCell.input2.delegate = self
return customCell
}
现在在视图控制器中实现了文本字段委托。
func textFieldDidEndEditing(_ textField: UITextField) {
guard let jobTaskCell = textField.superview?.superview as? CustomCell else {
return
}
if textField == jobTaskCell.input1 {
// Get text from textfield and store in array
} else if textField == jobTaskCell.input2 {
// Get text from textfield and store in array
}
}
注意:以下代码取决于如何在单元格中放置文本字段。因此,请确保您需要通过添加和删除 superView
来进行递归检查guard let jobTaskCell = textField.superview?.superview as? CustomCell else { return }
这只是意味着,仅当tableview单元内的文本字段没有任何额外的视图时:
textField.superview = TableViewCell的contentView
textField.superview?.superview = TableViewCell
我希望这能解决您的问题。
答案 1 :(得分:0)
这可以帮助您将文本存储在数组中:-
1:将 indexPath.row作为标签添加到cellForRowAt
中的textField中func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let customCell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
customCell.input1.tag = indexPath.row
customCell.input2.tag = indexPath.row
customCell.input1.delegate = self
customCell.input2.delegate = self
return customCell
}
2:在 textField委托方法中,您可以获取textField文本
func textFieldDidEndEditing(_ textField: UITextField) {
let indexPath = IndexPath(row: textField.tag, section: 0)
if let customCell = self.table.cellForRow(at: indexPath) as? CustomCell {
//From here you can get your particular input1 or input2 textfield text
print(customCell.input1.text)
print(customCell.input2.text)
}
}