I am building a kind of database and for this reason I have created a table view with custom cells so that users can insert the requested data into the appropriate text field. Here it is how it looks like till now.
Here it is the code that I have written for achieving this.
class customCell1: UITableViewCell {
@IBOutlet weak var cc1TextField1: UITextField!
@IBOutlet weak var cc1TextField2: UITextField!
@IBOutlet weak var cc1TextField3: UITextField!
@IBOutlet weak var cc1Label1: UILabel!
@IBOutlet weak var cc1Label2: UILabel!
@IBOutlet weak var cc1Label3: UILabel!
@IBOutlet weak var cc1MainLabel: UILabel!
}
In short, in a Swift file I have created a class, which I named customCell1
, while in the ViewController, which I called InputDataController
, I inserted these lines of code, where inputDataString
is the vector of strings concerning the different title that has to be assigned to each label in each row.
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return inputDataString.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell1 = tableView.dequeueReusableCell(withIdentifier: "myCell1", for: indexPath) as? customCell1
cell1?.cc1MainLabel.text = inputDataString[indexPath.row]
cell1?.cc1Label1.text = "Fabrics"
cell1?.cc1Label2.text = "Fittings"
cell1?.cc1Label3.text = "Furniture"
cell1?.cc1TextField1.placeholder = "Insert value here"
cell1?.cc1TextField2.placeholder = "Insert value here"
cell1?.cc1TextField3.placeholder = "Insert value here"
return cell1!
}
Now my question is, once the user added all these values in the text fields and clicked on the save button that I added on the navigation bar, how can I get all the data from the various textfields and assign them to a variable(s) so that I can use those values for some computation. And moreover, where do I add these lines of code so that everything works fine.
You can get all data by for
in action of button in the navigation bar:
let fabrics = [String]()
let fittings = [String]()
let furnitures = [String]()
for index in 0 ... inputDataString.count-1 {
fabrics.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField1.text)
fittings.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField2.text)
furnitures.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField3.text)
}