title: How to keep custom Selection formatting on UITableView when scroll
body: <p>I've got a TableView with static cells that are selectable (one can be selected at a time), and I've set a custom "SelectionView" so that when a cell is selected, a blue outline appears around it.</p>
  
  <p>The problem is when I either scroll OR leave the view and return (by going to another view controller), my custom formatting "breaks" in some way.  Sometimes it disappears (upon scrolling) and sometimes it seems to erase half of the outline (when i leave the view controller and return.)</p>
  
  <p>Is there a way to ensure that my custom selection view stays there?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>class BackgroundDesignController: UITableViewController {
  
      let selectionView = UIView()
      let defaults = UserDefaults.standard
      var  selectedBackground = "my default"
  
      @IBOutlet var myTableView: UITableView!
  
      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  
          if(indexPath == [0,0]){
              BackgroundName = "blank"
              NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
          }
          if(indexPath == [0,1]) {
              BackgroundName = "leaves"
              NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
          }
          if(indexPath == [0,2]) {
              BackgroundName = "water"
              NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
          }
          if(indexPath == [0,3]) {
              BackgroundName = "wood"
              NotificationCenter.default.post(name: NSNotification.Name("BackgroundDesignNotification"), object: nil)
          }          
      }
  
      func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
          if cell.isSelected {
              cell.isSelected = true
          } else {
              cell.isSelected = false
          }
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          //customize the selectedBackgroundView
          selectionView.layer.borderColor = UIColor.cyan.cgColor
          selectionView.layer.borderWidth = 6.0 //
          UITableViewCell.appearance().selectedBackgroundView = selectionView
      }
  
      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          //let indexPath = IndexPath(row: 1, section: 0)
  
          //reset the selected background  to the current value of the Background user default
          switch BackgroundName{
          case "blank":
              myTableView.selectRow(at: [0,0], animated: false, scrollPosition: .none)
              self.view.layoutIfNeeded()
          case "leaves":
              myTableView.selectRow(at: [0,1], animated: false, scrollPosition: .none)
              self.view.layoutIfNeeded()
          case "water":
              myTableView.selectRow(at: [0,2], animated: false, scrollPosition: .none)
              self.view.layoutIfNeeded()
          case "wood":
              myTableView.selectRow(at: [0,3], animated: false, scrollPosition: .none)
              self.view.layoutIfNeeded()
          default:
              print("There is a problem: no value for BackgroundName")
          }
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  }
  </code></pre>