Search code examples
iosswiftuitableviewcrashuisearchcontroller

Swift: Assertion failure when button clicked


Swift 3

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 (testArray) to Section 0 (followedArray). BUT when using the search bar to search through testArray and a row's button is clicked it crashes.

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

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

What am I doing wrong?

Here is the ERROR I get:

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

New Suggestion: 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?

Deleting from UISearchController's filtered search results

The code I'm using:

@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 && 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()

        }
    }
}

Solution

  • So I found the answer, I was adding the object to the array using insert at sections method instead of using just insert since I only have 1 section.

    let testObject: Test = filteredArray[indexPath.row]
    let indexOfObjectInArray = testArray.index(of: testObject)
    followedArray.insert(testObject, at: 0)