Swift:’尝试删除第0部分中的第0行,在更新之前只包含0行’

为什么我收到此错误? 我需要做什么?

*断言失败 – [UITableView _endCellAnimationsWithContext:],/ BuildRoot / Library / Cache / com.apple.xbs / Sources / UIKit / UIKit-3600.8.1 / UITableView.m:1442 2017-07-06 20:25:30.736267- 0400 BlogApp [1482:340583] *由于未捕获的exception’NSInternalInconsistencyException’而终止应用程序,原因:’尝试从更新之前仅包含0行的第0部分删除第0行’

崩溃就在这里

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

我从来没有使用数组数组var filteredArray = [[Blog]]()所以也许我只是没有正确访问它或正确删除它。

我刚刚发布了关于修复我正在使用的SearchBar问题的post,但是当我尝试它时,我遇到了这个崩溃。 当我在搜索对象时单击跟随按钮时会发生这种情况。 这是我的理解,因为我是SearchBar代码的新手。

从filteredArray中删除单元格时出现问题,可能无法正确访问它,因此无法删除它? 我已经逐行设置了断点,并且在从filteredArray中删除单元格时崩溃了

此外,我有一个SearchBar的一般问题,并获得了新的代码,所以这可能会有所帮助。

Swift:让SearchBar搜索两个部分而不是将它们组合起来

如有任何其他信息,请告诉我,谢谢。

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

我的其余代码,可能有助于解决问题

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