问题描述
我有一个 tableView
,用户可以在其中删除 cells
.这是在 cellForRowAt
中的 class WhishlistTableViewController: UITableViewController
:
I have a tableView
in which the user can delete cells
. This is in cellForRowAt
in class WhishlistTableViewController: UITableViewController
:
cell.deleteWishCallback = { [unowned self] deletedCell in
print("delete with callback")
guard let indexPath = tableView.indexPath(for: deletedCell) else { return }
self.deleteWishDelegate?.deleteWish(indexPath, currentWish.wishCounter)
self.wishData.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .right)
print("removed from tableView")
}
但我仍然使用 delegate
从数据库中删除 Wish
以及从 ViewController 内的主要
datasourceArray
这样我就可以将它安全地保存到 UserDefaults
:
But I am still using the delegate
to remove the Wish
from the database and also from my main datasourceArray
inside the ViewController
so I can safe it to UserDefaults
:
extension WishlistViewController: DeleteWishDelegate {
func deleteWish(_ idx: IndexPath, _ wishCounter: Int){
// remove the wish from the user's currently selected wishlist
print("delete2")
self.dataSourceArray[currentWishListIDX].wishes.remove(at: idx.row)
if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
defaults.setDataSourceArray(data: self.dataSourceArray)
defaults.synchronize()
} else {
print("error")
}
DataHandler.deleteWish(wishCounter, self.dataSourceArray[currentWishListIDX].name)
}
}
但是,如果我删除 cells
并再次添加,则会导致错误:
However this causes an error if I delete cells
and add again:
'无效更新:第 0 节中的行数无效.更新后现有节中包含的行数 (5) 必须等于更新前该节中包含的行数 (2),加上或减去从该部分插入或删除的行数(插入 1 行,删除 0 行),加上或减去移入或移出该部分的行数(0 移入,0 移出).'
我搜索了那个,当你没有按正确的顺序删除时,这通常会触发,但我认为我是按照正确的方式做的?(首先来自 tableViewData
(wishData),然后来自 tableView
本身和 deleteRows
.我在这里遗漏了什么?
I searched for that and this usually fire when you don't delete in the right order but I thought I am doing it the right way? (first from the tableViewData
(wishData) and then from the tableView
itself with deleteRows
. What am I missing here?
顺便说一下,删除 cell
时,这是 print-statements
的顺序:
By the way when deleting a cell
this is the order of the print-statements
:
使用回调删除
删除2
行数:2
从 tableView 中移除
removed from tableView
更新:
正如评论中所建议的,问题可能在于我如何添加 cells
.我就是这样做的:
As suggested in the comments, the problem might be in how I add cells
. This is how I do that:
func addWishComplete(wishName: String, selectedWishlistIDX: Int, wishImage: UIImage?, wishLink: String?, wishPrice: String?, wishNote: String?, wishCounter: Int?) {
let wishToAdd = Wish(name: wishName, link: wishLink!, price: wishPrice!, note: wishNote!, image: wishImage!, checkedStatus: false, wishCounter: wishCounter!)
self.dataSourceArray[selectedWishlistIDX].wishes.append(wishToAdd)
DataHandler.saveWish(dataSourceArray: self.dataSourceArray, selectedWishlistIdx: selectedWishlistIDX, wish: wishToAdd)
// save dataSourceArray with new wish in UserDefaults
if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
defaults.setDataSourceArray(data: self.dataSourceArray)
defaults.synchronize()
} else {
print("error wish to datasource")
}
// only update current list if selectedWishlist is currentWishlist
if selectedWishlistIDX == currentWishListIDX {
wishList.wishes.append(Wish(name: wishName, link: wishLink!, price: wishPrice!, note: wishNote!, image: wishImage!, checkedStatus: false, wishCounter: wishCounter!))
theTableView.wishData = wishList.wishes
theTableView.tableView.beginUpdates()
theTableView.tableView.insertRows(at: [
(NSIndexPath(row: theTableView.wishData.count-1, section: 0) as IndexPath)], with: .left)
theTableView.tableView.endUpdates()
}
dismissWishView()
}
推荐答案
我发现了问题.正如评论中已经提到的,我必须可以直接在 wishData
上工作以 remove
:
I found the problem. As already mentioned in the comments I have to can work directly on wishData
to remove
:
func addWishComplete(wishName: String, selectedWishlistIDX: Int, wishImage: UIImage?, wishLink: String?, wishPrice: String?, wishNote: String?, wishCounter: Int?) {
let wishToAdd = Wish(name: wishName, link: wishLink!, price: wishPrice!, note: wishNote!, image: wishImage!, checkedStatus: false, wishCounter: wishCounter!)
self.dataSourceArray[selectedWishlistIDX].wishes.append(wishToAdd)
DataHandler.saveWish(dataSourceArray: self.dataSourceArray, selectedWishlistIdx: selectedWishlistIDX, wish: wishToAdd)
// save dataSourceArray with new wish in UserDefaults
if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
defaults.setDataSourceArray(data: self.dataSourceArray)
defaults.synchronize()
} else {
print("error wish to datasource")
}
// only update current list if selectedWishlist is currentWishlist
if selectedWishlistIDX == currentWishListIDX {
wishList.wishes.append(wishToAdd)
theTableView.wishData.append(wishToAdd)
theTableView.tableView.beginUpdates()
theTableView.tableView.insertRows(at: [
(NSIndexPath(row: theTableView.wishData.count-1, section: 0) as IndexPath)], with: .left)
theTableView.tableView.endUpdates()
}
dismissWishView()
}
感谢大家的帮助!
这篇关于即使我按顺序删除,部分中的行数也无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!