Conditionally Adding and Deleting a Row in a UITableView

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!

Answered by Claude31 in 841559022

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.

It is not very clear.

Could you post a sketch showing:

  • what are the options (custom and another)? Are there 2 buttons ? Or only one button which behaviour should change depending on the slider value ?
  • which another option is it ?
  • Is the TextField inside TableView row ?

when the custom option is tapped, bring up a row immediately below there and have a UITextField

  • You bring up a new row ? Or an existing one ?
  • do you add a TextField, or was it already in the row ?

When another option, let's say 10%, is tapped, I want the text field row to go away.

Do you want to delete the row ? Or just hide it ?

If there is only one button which behaviour change depending on the slider value:

  • in the button action, you test the value of slider
  • if 20% or more, then call mytableView.insertRows(at…)
  • if less, then call mytableView.deleteRows(at…)

In both cases, don't forget to update the dataSource.

My tip percentage slider is a Segmented Control, with options ranging from 0% to 20% and then 'Any' is the custom value. When the user selects the 'Any' value, I want to be able to have a TextField appear below it, and then disappear when the user selects a value other than 'Any'.

if (isTipPercentageCustom) {
// Show the Text Field
// Perform calculation, etc.
} else {
// Hide Text Field
}

I want to create it once, and then just keep showing and hiding it since I have to keep it in memory until the app closes.

Is this even possible with a Segmented Control?

Does that clarify it a little bit?

Accepted Answer

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.

Conditionally Adding and Deleting a Row in a UITableView
 
 
Q