title: Swift - Custom SectionIndexTitles not working in Tableview
body: <p>I have a UISplitViewController of which I have replaced the UITableView with a UIViewController. This ViewController contains a UITableView and a UIView. The UIView contains my custom SectionIndexTitles UIControl, to scroll to the right section.</p>
  
  <p>My custom SectionIndexTitles UIControl works perfectly in a ViewController with a UICollectionView but somehow it doesn't work in the configuration mentioned before. How can I solve this?</p>
  
  <p>The app compiles and doesn't crash. When I scrub the UIControl, the indexViewValueChanged() function doesn't get called. I have checked the View hierarchy using the View Debugger, but it's above the TableView. I also tried adding a Gesture Recognizer, which did work. Lastly I tried setting the zPosition, but this didn't have any effect.</p>
  
  <p><a href="https://github.com/evenwerk/CustomSectionIndexTitlesTest?files=1" rel="nofollow noreferrer">Github example</a></p>
  
  <p><strong>The ViewController</strong></p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      @IBOutlet weak var tableView: UITableView!
      @IBOutlet weak var indexTitlesContainer: UIView!
  
      /* type to represent table items
      `section` stores a `UITableView` section */
      class User: NSObject {
          let name: String
          var section: Int?
  
          init(name: String) {
              self.name = name
          }
      }
  
      // custom type to represent table sections
      class Section {
          var users: [User] = []
  
          func addUser(user: User) {
              self.users.append(user)
          }
      }
  
      // raw user data
      let names = [
          "Clementine",
          "Tim",
          "Bessie",
          "Yolande",
          "Tynisha",
          "Ellyn",
          "Trudy",
          "Fredrick",
          "Letisha",
          "Ariel",
          "Bong",
          "Jacinto",
          "Dorinda",
          "Aiko",
          "Loma",
          "Augustina",
          "Margarita",
          "Jesenia",
          "Kellee",
          "Annis",
          "Charlena"
      ]
  
      // `UIKit` convenience class for sectioning a table
      let collation = UILocalizedIndexedCollation.currentCollation()
  
  
      // table sections
      var sections: [Section] {
          // return if already initialized
          if self._sections != nil {
              return self._sections!
          }
  
          // create users from the name list
          let users: [User] = names.map { name in
              let user = User(name: name)
              user.section = self.collation.sectionForObject(user, collationStringSelector: "name")
              return user
          }
  
          // create empty sections
          var sections = [Section]()
          for i in 0..&lt;self.collation.sectionIndexTitles.count {
              sections.append(Section())
          }
  
          // put each user in a section
          for user in users {
              sections[user.section!].addUser(user)
          }
  
          // sort each section
          for section in sections {
              section.users = self.collation.sortedArrayFromArray(section.users, collationStringSelector: "name") as! [User]
          }
  
          self._sections = sections
  
          return self._sections!
  
      }
      var _sections: [Section]?
  
      var indexTitles: IndexTitles!
  
      override func viewDidLoad() {
          super.viewDidLoad()
          // Do any additional setup after loading the view, typically from a nib.
  
          indexTitles = IndexTitles(frame: CGRectZero, indexTitles: collation.sectionIndexTitles)
  
          indexTitles.addTarget(self, action: "indexViewValueChanged:", forControlEvents: .ValueChanged)
          indexTitlesContainer.addSubview(indexTitles)
  
      }
  
      func indexViewValueChanged(sender: IndexTitles) {
          print("perform function")
          var index = sender.index
  
          if self.sections[index].users.count == 0 {
  
              var shouldPerform = true
  
              for var i = index as Int; i &lt; self.sections[index].users.count; i++ {
                  if shouldPerform == true {
                      if self.sections[index].users.count != 0 {
                          shouldPerform = false
                          index = i
                      }
                  }
              }
  
              for var i = index as Int; i &gt; 0; i-- {
                  if shouldPerform == true {
                      if self.sections[index].users.count != 0 {
                          shouldPerform = false
                          index = i
                      }
                  }
              }
          }
  
          let path = NSIndexPath(forRow: 0, inSection: index)
          tableView.scrollToRowAtIndexPath(path, atScrollPosition: .Top, animated: false)
  
          let scrollPosition = tableView.contentOffset.y
          let collectionViewHeight = (tableView.contentSize.height - tableView.bounds.size.height) + 54
  
          if scrollPosition != collectionViewHeight {
              tableView.contentOffset = CGPoint(x: tableView.contentOffset.x, y: tableView.contentOffset.y - 22.0)
          }
  
      }
  
      override func viewDidLayoutSubviews() {
          let height = CGFloat(collation.sectionIndexTitles.count * 18)
          let y = (indexTitlesContainer.bounds.height - height) / 3
          indexTitles.frame = CGRectMake(0, y, indexTitlesContainer.bounds.width, height)
  
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
  
  }
  
  extension ViewController:UITableViewDataSource {
      func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          return self.sections.count
      }
  
      func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          return self.sections[section].users.count
      }
  
      func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -&gt; String? {
          return collation.sectionIndexTitles[section]
      }
  
      func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
          let user = self.sections[indexPath.section].users[indexPath.row]
  
          let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
          cell.textLabel!.text = user.name
          cell.backgroundColor = UIColor.clearColor()
          return cell
      }
  }
  </code></pre>
  
  <p><strong>The Custom UIControl</strong></p>
  
  <pre><code>import UIKit
  
  class IndexTitles: UIControl,UIGestureRecognizerDelegate {
  
      var indexTitles:NSArray!
      var visualEffect: UIVisualEffectView!
      var index:Int!
  
      func translate(touch:CGFloat) {
          let selectedIndex = Int(touch / 18)
          let indexCount = indexTitles.count - 1
  
          if selectedIndex &lt; 0 {
              index = 0
          }
          else if selectedIndex &gt; indexCount {
              index = indexCount
          }
          else {
              index = selectedIndex
          }
  
          sendActionsForControlEvents(.ValueChanged)
      }
  
      //Sets the right index when the label is selected.
      override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -&gt; Bool {
          translate(touch.locationInView(self).y)
          return true
      }
  
      override func continueTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -&gt; Bool {
          translate(touch.locationInView(self).y)
          return true
      }
  
      //Setup for when the view is being initialized.
      init(frame: CGRect, indexTitles:NSArray) {
          super.init(frame: frame)
          self.indexTitles = indexTitles
  
          let multiplier = 18
  
          visualEffect = UIVisualEffectView(frame: CGRectMake(0, 0, 18, CGFloat(multiplier * indexTitles.count)))
          visualEffect.effect = UIVibrancyEffect(forBlurEffect: UIBlurEffect(style: .ExtraLight))
          self.addSubview(visualEffect)
  
          for var i = 0; i &lt; indexTitles.count; i++ {
              let label = UILabel(frame: CGRectMake(0, CGFloat(multiplier * i) , 20 , 18))
              label.font = UIFont.systemFontOfSize(12, weight: UIFontWeightSemibold)
              label.numberOfLines = 0
              label.textAlignment = .Center
              label.text = indexTitles[i] as? String
              visualEffect.contentView.addSubview(label)
          }
  
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
      }
  
  
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/UR1yQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UR1yQ.png" alt="The SplitViewController with TableView and custom UIControl"></a></p>