为什么我收到此错误?我需要做什么?
*断言失败 - [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] * 由于未捕获的异常'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问题的帖子,但是当我尝试它时,我遇到了这个崩溃。当我在搜索对象时单击跟随按钮时会发生这种情况。我不太了解,因为我是SearchBar代码的新手。
从filteredArray中删除单元格时出现问题,可能无法正确访问它,因此无法删除它?我已经逐行设置了断点,并且在从filteredArray
中删除单元格时崩溃了另外,我遇到了SearchBar的一般问题并获得了新代码,所以这可能会有所帮助。
Swift: Have SearchBar search through both sections and not combine them
如有任何其他信息,请告诉我,谢谢。
// 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()
}
}
答案 0 :(得分:1)
您必须首先从TableView中删除单元格!
// ----- Removing Cell from filteredArray -----
self.myTableView.deleteRows(at: [indexPath], with: .fade)
filteredArray.remove(at: [indexPath.section][indexPath.row])
mainArray.remove(at: indexOfObjectInArray!)
尝试一下。
答案 1 :(得分:0)
您可以尝试使用不同的操作更新 tableview。
喜欢:
- 执行您的阵列更新过程-
- 对于插入过程-
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()
希望它会有所帮助,因为我遇到了同样的问题,这个解决方案对我有用!!