I have crated this tableview to obtain the users children's names and store them into an array called dependentsArray
:
I have searched through stackoverflow, Apple documentation, google, and youtube tutorials, and cant seem to find a solution. This seems like it should be simple but its causing quite the headache. The closest i found to a solution was this post:
getting data from each UITableView Cells Swift
However, i am not obtaining the cells textfield value by tapping them, so i cannot use didDeselectRowAtIndexPath
. I wish to obtain the text values when i tap the "Next" button.
The tableview expands and contracts its cell count based on an integer called dependentsCount
. I increment and decrement the dependentsCount
like so:
@IBAction func subtractDependentButtonClick(sender: AnyObject) {
dependentsCount -= 1
dependentsTableView.reloadData()
fadeTransition(0.3)
}
@IBAction func addDependentButtonClick(sender: AnyObject) {
dependentsCount += 1
dependentsTableView.reloadData()
fadeTransition(0.3)
}
The ways in which ive tried to obtain the value of text fields are as follows:
@IBAction func nextButtonPress(sender: AnyObject) {
var index = 0
var cell = self.dependentsTableView.cellForRowAtIndexPath(NSIndexPath(index: index)) as! DependentsTableViewCell
while index < dependentsCount
{
self.dependentsArray.append(cell.nameTextField.text!)
index += 1
}
self.performSegueWithIdentifier("showNextVC", sender: nil)
}
which crashed the app ^^
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let cell = dependentsTableView.dequeueReusableCellWithIdentifier("Cell") as! DependentsTableViewCell
dependentsArray.append(cell.nameTextField.text!)
print("\(dependentsArray)")
}
which does nothing ^^
what is the correct way to obtain tableview cell's textfield text?