Swift:“尝试从第0部分删除行0,该部分仅包含更新前的0行" [英] 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?
*-[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] * 由于未捕获的异常"NSInternalInconsistencyException"而终止应用程序,原因:试图从之前仅包含0行的第0部分删除第0行更新"
* 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'
崩溃在这里
// ----- 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]]()
的数组,所以也许我只是不正确地访问它或正确地删除它.
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.
我刚刚发布了有关解决我的SearchBar问题的信息,但是在尝试时,我遇到了崩溃问题.当我在搜索对象时单击跟随"按钮时,就会发生这种情况.由于我是SearchBar代码的新手,这超出了我的理解.
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.
从filteredArray删除单元格时出现问题,可能无法正确访问它,因此无法将其删除?我已经逐行设置了断点,并且在从filteredArray删除单元格时崩溃了
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
此外,我在SearchBar上遇到了一个一般性问题,并获得了新代码,因此也许可以帮上忙.
Also, I had a general problem with SearchBar and got new code so maybe this can help.
迅速:让SearchBar在两个部分中进行搜索,而不是将它们组合在一起
有关其他任何信息,请让我知道,谢谢.
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
}
取消关注按钮代码
这里的问题应该与跟随"按钮的相反.
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()
}
}
推荐答案
您必须首先从TableView中删除单元格!
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!)
尝试一下.
这篇关于Swift:“尝试从第0部分删除行0,该部分仅包含更新前的0行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!