gpt4 book ai didi

ios - UITableView deleteRows 未更新 IndexPaths 导致崩溃索引超出范围

转载 作者:行者123 更新时间:2023-12-01 19:29:29 25 4
gpt4 key购买 nike

在 API 调用成功时调用以下代码以从列表中删除项目

self?.myWishlistArry?.remove(at: indexPath.row)
self?.myWishListTableView.deleteRows(at: [indexPath], with: .right)
以下是删除按钮的关闭
        myWishlistCell.closureForRemoveBtn = {
[weak self]
(productId, storeid) in
self?.callAlertViewForRemoved(productid: productId, storeId: storeid, indexPath: indexPath)
}
以下是显示用户确认警报的代码。
    func callAlertViewForRemoved(productid :String, storeId: String, indexPath: IndexPath) {


let alertController = UIAlertController(title: AppLocalString.ITEM_NOT_AVAILABLE_TEXT.localized(), message: AppLocalString.MY_EXLUDE_TEXT.localized(), preferredStyle: .alert)

let okAction = UIAlertAction(title: AppLocalString.ALERT_YES.localized(), style: UIAlertAction.Style.default) {
UIAlertAction in
if self.listBool == false {
//self.listBool = true
self.callRemoveWishlist(productid :productid, storeId: storeId, indexPath: indexPath)
}
}

let cancelAction = UIAlertAction(title: AppLocalString.Cancel_ORDER.localized(), style: UIAlertAction.Style.cancel) {
UIAlertAction in
NSLog("Cancel Pressed")
}

// Add the actions
alertController.addAction(okAction)
alertController.addAction(cancelAction)

self.present(alertController, animated: true, completion: nil)


}
以下是代码 api 删除
    func callRemoveWishlist(productid :String, storeId: String, indexPath: IndexPath)  {

listBool = true
addLoading(view: view, random: removeRandom)
let addFriendsBaseUrl = AppStrings.baseUrl + AppStrings.addToWishlistAPI
var paramDict = [String : Any]()

paramDict = ["type" : "remove",
"productid" : productid,
"storeid" : "\(storeId)",
"userid" : UserDefaults.standard.value(forKey: AppStrings.userid) ?? ""]


let baseParams = Utility.appendBaseParams(toDict: paramDict as! [String : String])

NetworkResponseManager.getDataFromUrl(url: addFriendsBaseUrl, parameters: baseParams as! [String : String] , completionHandler: {
[weak self]
(response, error) in
self?.listBool = false
if (error == nil) {

if let responseDict = response {

let responseDict = AddToWishlistModel(JSON(responseDict))
self?.removeLoading(view: self?.view, random: self?.removeRandom)
if responseDict.status == 1
{
self?.myWishlistArry?.remove(at: indexPath.row)
self?.myWishListTableView.deleteRows(at: [indexPath], with: .right)
} else {
self?.showToast(message: AppLocalString.OOPS_TEXT.localized())
}
}
} else {
self?.showToast(message: AppLocalString.OOPS_TEXT.localized())
}
})
}
其他 TableView 代码
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.myWishlistArry?.count ?? 0
}


func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
现在这段代码第一次工作但如果尝试删除另一个说索引超出范围的项目会导致崩溃。考虑列表中是否有四个项目 T1、T2、T3 和 T4
第一次如果 我删除 T2 item indexPath.row 值为 1 现在 item 和 row 被删除 如果我删除另一个项目说 T4 的值indexPath.row 是 3 而不是 2 为什么会发生这种情况 为什么 indexpaths 没有更新?

最佳答案

根据整个代码,看起来问题出在clojure
如果您在 cellForRowAt 中的 clojure 中调用它,则索引路径是不同的(设置为在调用 cellForRowAt 时正确的值)。您应该根据愿望 list 项目的 id 进行删除(删除项目时不会改变的东西)。
例如创建一个方法,如:

func deleteWish(productId: string) {
for i in myWishlistArry ?? [] {
let product = myWishlistArry[i]
if let product.productId == productId {
myWishlistArry?.remove(at: i)
myWishListTableView.deleteRows(at: [IndexPath(row: i, 0)], with: .right)
}
}

}
并调用clojure deleteWish(productId: idOfDeletedProduct)

关于ios - UITableView deleteRows 未更新 IndexPaths 导致崩溃索引超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63831476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com