Swift: 'Versuch zum löschen der Zeile 0 aus Abschnitt 0 enthält nur 0 Zeilen vor dem update"

Warum bin ich immer diese Fehlermeldung? Was muss ich tun?

* Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Quellen/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: 'Versuch zum löschen der Zeile 0 aus Abschnitt 0 enthält nur 0 Zeilen vor dem update'

Absturz ist hier

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

Habe ich noch nie gearbeitet mit einem array von arrays var filteredArray = [[Blog]]() also vielleicht bin ich einfach nicht auf es zu Recht oder zu löschen Recht.

Hatte ich gerade gepostet über die Festsetzung meine Suchleiste problem ich hatte, aber als ich versuchte, es heraus, ich lief in dieses crash. Es passiert, wenn ich auf den Folgen button bei der Suche für ein Objekt. Es ist jenseits meines Verständnisses, wie ich bin neu in der Suchleiste code.

Es ist ein Problem beim entfernen der Zelle aus filteredArray, vielleicht nicht auf es zu Recht so kann es nicht löschen? Ich hatte die Einrichtung von Haltepunkten Zeile für Zeile und es stürzt ab beim entfernen der Zelle aus filteredArray

Außerdem hatte ich ein generelles problem mit Suchleiste und bekam neue code, also vielleicht kann das helfen.

Swift: Haben Suchleiste Suche durch beide Abschnitte und nicht kombinieren

Für andere Informationen, bitte lassen Sie mich wissen, danke.

//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 von meinem code, vielleicht hilft das bei der Behebung des Problems

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

Gleiche problem haben sollte hier, wie Ihr das Gegenteil von dem Folgen-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()
    }
}
  • Vielleicht nicht verwandt, aber nie rufen Sie reloadData nach insert/delete/move Betrieb. insert/delete/move behandelt die UI aktualisiert und Sie verlieren die animation.
  • Wo ist am besten zu setzen reloadData? Auch bin ich mit der Formulierung dieser Beitrag Recht, hoffentlich verstehen die Menschen?
  • Löschen aller reloadData() Zeilen direkt nach insertRows/deleteRows/moveRows
  • wie wird die tableview aktualisiert dann?
  • Wieder einmal einfügen/löschen/verschieben Griffen die UI update
  • Ja, Apple ist sehr gut über diese Sachen: Methoden tun, was die Namen sagen, die Methoden zu tun. deleteRows löschen der Zeilen. So, nachdem Sie es nennen, werden die Zeilen gelöscht. Sie haben gesagt, alles, was Sie brauchen, um zu sagen; Sie haben gesagt, der Tisch-Ansicht alles, was es tun muss. Warum würden Sie brauchen, um dazu mehr sagen?
  • Hat nicht wirklich etwas ändern, so war es interessant, aber gut zu lernen die kleinen Dinge, die Sie verbessern können

InformationsquelleAutor WokerHead | 2017-07-07
Schreibe einen Kommentar