Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
516 views
in Technique[技术] by (71.8m points)

ios - Swift: 'attempt to delete row 0 from section 0 which only contains 0 rows before the update'

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

* Assertion failure 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] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete row 0 from section 0 which only contains 0 rows before the update'

Crash is 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 right or deleting it right.

I just had posted about fixing my SearchBar problem I was having, but as I was trying it out, I ran into this crash. It happens when I click the follow button when searching for an object. It's beyond my understanding as I am new to the SearchBar code.

It's having an issue when removing the cell from filteredArray, maybe not accessing it right so it can't delete it? I had set up breakpoints line by line and it crashes in when removing the cell from filteredArray

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

Swift: Have SearchBar search through both sections and not combine them

For any other information please let me know, thank you.

// 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()
        }
    }
}

Rest of my code, maybe helps with 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
}

Unfollow Button Code

Same problem should be here as its 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()
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to delete the cell from TableView first!

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

Try it out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...