Swift: Assertion failure when button clicked
<p>Swift 3</p>

<p>I have a table view with several rows, each row has a button (uses indexPath.row), when the button is clicked it moves the row from Section 1 (<em>testArray</em>) to Section 0 (<em>followedArray</em>). BUT when using the search bar to search through testArray and a row's button is clicked it crashes.</p>

<p>Now when using the search bar, <em>testArray</em> is filtered into a new array called <em>filteredArray</em>. The button is supposed to move the row from <em>filteredArray</em> to <em>followedArray</em>.</p>

<p>Without searching, the button works as intended, it only crashes when using the search bar.</p>

<p>What am I doing wrong?</p>

<p><strong>Here is the <em>ERROR</em> I get:</strong></p>

<pre><code>CustomCellSwift[1473:391841] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3600.5.2/UITableView.m:1315
</code></pre>

<p><strong>New Suggestion:</strong> Could it have anything to do with it having a conflict with the indexPath.row of testArray and followedArray when filteredArray replaces the table view?</p>

<blockquote>
  <p><a href="https://stackoverflow.com/questions/27656678/deleting-from-uisearchcontrollers-filtered-search-results">Deleting from UISearchController&#39;s filtered search results</a></p>
</blockquote>

<p><em>The code I'm using:</em></p>

<pre><code>@IBAction func followButtonClick(_ sender: UIButton!) {

    // Adding row to tag
    let buttonPosition = (sender as AnyObject).convert(CGPoint.zero, to: self.myTableView)
    if let indexPath = self.myTableView.indexPathForRow(at: buttonPosition) {

        // Change Follow to Following
        (sender as UIButton).setImage(UIImage(named: "follow.png")!, for: .normal)
        cell.followButton.isHidden = true
        cell.followedButton.isHidden = false

        // Checking wether to import from testArray or filteredArray to followedArray
        if !(searchController.isActive &amp;&amp; searchController.searchBar.text != "") {

            self.myTableView.beginUpdates()

            // ----- Inserting Cell to followedArray -----
            followedArray.insert(testArray[indexPath.row], at: 0)
            myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)

            // ----- Removing Cell from testArray -----
            testArray.remove(at: indexPath.row)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 1)], with: .fade)

            self.myTableView.endUpdates()

        }
        else {

            self.myTableView.beginUpdates()

            // ----- Inserting Cell to followedArray -----
            followedArray.insert(filteredArray[indexPath.row], at: 0)
            myTableView.insertRows(at: [IndexPath(row: 0, section: 0)], with: .fade)

            // ----- Removing Cell from filteredArray -----
            filteredArray.remove(at: indexPath.row)
            let rowToRemove = indexPath.row
            self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)

            self.myTableView.endUpdates()

        }
    }
}
</code></pre>