Hello!
I wanted to see if someone with more UIKit experience than me can help me out on guiding me in the right direction for conditionally adding and deleting a row in a UITableView.
What I Want to Accomplish
I have a tip slider with percentages (0% - 20%) with a custom option on the end. I'm wanting to, when the custom option is tapped, bring up a row immediately below there and have a UITextField. When another option, let's say 10%, is tapped, I want the text field row to go away.
Can someone explain to me how this would work? And if so, provide an example?
Thank you!
That's clearer. You are not adding or deleting row, but show or hide a TextField.
- Declare in storyboard a UITextField, positioned just below the segmented control. Declare it as hidden in the storyboard. Define its delegate (the viewController). Define placeholder as: enter custom tip
- declare an IBOutlet for this UITextField : tipTextField for instance
- there is an IBAction associated to the SegmentedControl (valueChanged event)
- in the IBAction, test for the selection
@IBAction func segmentSelection(_ sender: UISegmentedControl) {
let selection = sender.selectedSegmentIndex
if selection == sender.numberOfSegments - 1 { // The last segment = Any
tipTextField.isHidden = false
} else {
tipTextField.isHidden = true
// store the relevant value in a tipValue property
}
}
You should also define and IBAction for the textField, (for EditingDidEnd and for didEndOnExit), that will read the text you have entered and convert to Int to get the tip value. Then save this value in a tipValue property of the ViewController.
Hope that helps. Don't forget to close the thread if OK. Otherwise, explain what you are missing.