title: Swift slow custom keyboard buttons with collectionviews
body: <h1>Disclaimer:</h1>
  <p>Before you rip my head off and set it on fire, <strong>I know questions like this have already been asked but they all refer to single buttons</strong>. <strong>I have a collectionView</strong>. I'm asking this question after carefully reading all the other questions and trying to combine their solutions together.</p>
  <h1>Overview:</h1>
  <p>I'm creating a keyboard extension with a collectionview. In my collectionView I have custom cells and in their class I placed a button. I want to add a target to each button in the collection view. I know I could use the selectItemAtIndexRow function and ignore the buttoms but I need to handle the touchUpInside and touchDown events (because when I type with y keyboard it's super slow) and I haven't found a way to do it with collectionView cells (if something exists let me know). In order to escape this problem, I thought the best solution could have been adding a button to my cell's class and add the actions I wanted to it, but I found multiple prooblems doing so.</p>
  <h1>What I'm doing:</h1>
  <ol>
  <li><p>I have the classic keyboardViewController you get by creating a new keyboard target where I placed a view (which is the view containing the collectionView).</p>
  </li>
  <li><p>I have a custom view  class which contains the collection view</p>
  </li>
  <li><p>I have a custom collectionview cell class</p>
  </li>
  </ol>
  <h3>Custom View Class</h3>
  <p>Here I programmatically created my collectionView.</p>
  <pre class="lang-swift prettyprint-override"><code>//Closure to add action to my buttons
   var insertLowercase : ((IndexPath) -&gt; ())?
      
   let letters = [&quot;q&quot;, &quot;w&quot;, &quot;e&quot;, &quot;r&quot;, &quot;t&quot;, &quot;y&quot;, &quot;u&quot;, &quot;i&quot;, &quot;o&quot;, &quot;p&quot;]
  
   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          letters.count
      }
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = keyView.dequeueReusableCell(withReuseIdentifier: &quot;collectionCellId&quot;, for: indexPath) as! KeyboardKeys
          cell.button.setTitle(letters[indexPath.row], for: .normal)
          cell.button.addTarget(self, action: #selector(lettersKeyboard.insert(indexPath:)), for: .touchUpInside)
          return cell
      }
      
      @objc func insert (indexPath: IndexPath){
          insertLowercase?(indexPath)
      }
  </code></pre>
  <p>This is how I add the target. However if I press the button, the keyboard crashes giving me the following error:</p>
  <blockquote>
  <p>Thread 1: &quot;-[Keyboarddd.KeyboardButton length]: unrecognized selector sent to instance 0x7f9bb2506740&quot;</p>
  </blockquote>
  <h3>Keyboard ViewController</h3>
  <p>After putting here my custom view, in the viewDidLoad I call this:</p>
  <pre class="lang-swift prettyprint-override"><code> letters.insertLowercase = { indexPath in
              let text = self.letters.letters[indexPath.row]
              self.textDocumentProxy.insertText(text)
          } //And more stuff to handle quick insertion but this is what you need to reproduce my problem
  </code></pre>
  <h1>Question:</h1>
  <p>How can I decently add the target to my button? Or is there a way to do what I want to do with the collectionView cells directly?</p>