Swift: "try to remove line 0 from section 0 that only contains 0 lines before updating",

Why am I getting this error? What do I need to do?

* Assertion error in - [UITableView _endCellAnimationsWithContext:], / BuildRoot / Library / Caches / com.apple.xbs / Sources / UIKit / UIKit-3600.8.1 / UITableView.m: 1442 2017-07-06 20:25: 30.736267- 0400 BlogApp [1482: 340583] * Application terminated due to unreported 'NSInternalInconsistencyException', reason: 'try to delete line 0 from section 0 which contains only 0 lines before update'

Crash here

// ----- Inserting Cell to followedArray -----
let blogObject: Blog = filteredArray[indexPath.section][indexPath.row]
let indexOfObjectInArray = mainArray.index(of: blogObject)

followedArray.insert(blogObject, at: 0)

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

self.myTableView.endUpdates()

      

I've never worked with an array of arrays var filteredArray = [[Blog]]()

, so maybe I'm just not accessing it correctly or deleting it correctly.

I just posted a post about fixing my SearchBar problem I had, but as I tried it I ran into this disaster. This happens when I click the Next button while searching for an object. This is beyond my grasp as I am new to SearchBar code.

It has a problem when removing a cell from filterArray, it may not be able to access it correctly, so it cannot remove it? I have set breakpoints line by line and it crashes when removing a cell from filterArray

Also, I had a general problem with SearchBar and got new code, so maybe this can help.

Swift: find SearchBar through both sections and don't merge them

For any other information please let me know, thanks.

// Follow Button
@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 mainArray or filteredArray to followedArray
        if !(searchController.isActive && searchController.searchBar.text != "") {

            self.myTableView.beginUpdates()

            // Save identifier into followedIdentifier array
            self.followedIdentifiers.insert(mainArray[indexPath.row].blogID)

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

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

            self.myTableView.endUpdates()

            // After Updating Table, Save the Archived Data to File Manager
            saveData()
        }
        else { // **** Crash in this section ****

            self.myTableView.beginUpdates()

            // Remove identifier into followedIdentifier array
            self.followedIdentifiers.remove(followedArray[indexPath.row].blogID)

            // ----- Inserting Cell to followedArray -----
            let blogObject: Blog = filteredArray[indexPath.section][indexPath.row]
            let indexOfObjectInArray = mainArray.index(of: blogObject)

            followedArray.insert(blogObject, at: 0)

            //------------------------
            // CRASH SHOULD BE HERE (breakpoints lead me here)
            //------------------------

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

            self.myTableView.endUpdates()

            // After Updating Table, Save the Archived Data to File Manager
            saveData()
        }
    }
}

      

The rest of my code probably helps in fixing the problem.

var mainArray = [Blog]()
var followedArray = [Blog]()
var filteredArray = [[Blog]]()

// Number of Rows in Section
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if !(searchController.isActive && searchController.searchBar.text != "") {

        if section == 0 {
            return followedArray.count
        } else {
            return mainArray.count
        }
    } else {
        return filteredArray[section].count
    }
}

// CellForRowAt indexPath
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let CellIdentifier = "Cell"
    var cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as! CustomCell

    if cell != cell {
        cell = CustomCell(style: UITableViewCellStyle.default, reuseIdentifier: CellIdentifier)
    }

    // Configuring the cell
    var blogObject: Blog

    if !(searchController.isActive && searchController.searchBar.text != "") {
        if indexPath.section == 0 {
            blogObject = followedArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: true, indexPath: indexPath, parentView: self)
        } else {
            blogObject = mainArray[indexPath.row]
            cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
        }
    } else {
        blogObject = filteredArray[indexPath.section][indexPath.row]
        cell.populateCell(blogObject, isFollowed: false, indexPath: indexPath, parentView: self)
    }

    return cell
}

      

Disable button code

The same problem should be here as the opposite of the follow button.

// Unfollow Button
@IBAction func followedButtonClick(_ 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 Following to Follow
        (sender as UIButton).setImage(UIImage(named: "followed.png")!, for: .normal)
        cell.followButton.isHidden = false
        cell.followedButton.isHidden = true

        self.myTableView.beginUpdates()

        // Remove identifier into followedIdentifier array
        self.followedIdentifiers.remove(followedArray[indexPath.row].blogID)

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

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

        self.myTableView.endUpdates()

        // After Updating Table, Save the Archived Data to File Manager
        saveData()
    }
}

      

+3


source to share





All Articles