As the title said; I want to read the data from multiple UITextField
s for each cell and store them in an array. How would I do so?
I have created a subclass CustomCell
that has 2 UITextFields
in it. P/S: the identifier for the cell is also CustomCell
Many thanks
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 class:
Import UIKit
public class CustomCell: UITableViewCell {
@IBOutlet weak var input1: UITextField!
@IBOutlet weak var input2: UITextField!
}