斯威夫特:'attempt to delete row 0 from section 0 which only contains 0 rows before the update'



为什么会出现此错误?我需要做什么?

* 断言失败 -[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 行">

崩溃来了

// ----- 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]]()所以也许我只是没有正确访问它或删除它。

我刚刚发布了有关解决我遇到的搜索栏问题的信息,但是当我尝试它时,我遇到了这次崩溃。当我在搜索对象时单击关注按钮时,就会发生这种情况。这超出了我的理解,因为我是搜索栏代码的新手。

从filteredArray中删除单元格时出现问题,也许无法访问它,因此无法删除它?我已经逐行设置了断点,从filteredArray中删除单元格时它崩溃了

另外,我在搜索栏上遇到了一个普遍的问题,并获得了新代码,所以也许这会有所帮助。

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

您必须先从 TableView 中删除单元格!

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

试试吧。

您可以尝试使用不同的操作更新表视图。

喜欢:

-执行阵列更新过程 -

-用于插入过程 -

self.myTableView.beginUpdates()
myTableView.insertRows(at: [IndexPath(row: 0, section: 1)], with: .fade)
self.myTableView.endUpdates()

然后

-执行阵列更新过程 -

-删除过程 -

self.myTableView.beginUpdates()
self.myTableView.deleteRows(at: [IndexPath(row: rowToRemove, section: 0)], with: .fade)
self.myTableView.endUpdates()

希望它会有所帮助,因为我遇到了同样的问题,这个解决方案对我有用!!

相关内容

最新更新