为什么我不能在swift中再删除一个带有JSON参数的CollectionVIew Cell



在我的视图控制器中,我有两个JSON API 1(用于向collectionView单元格添加图像和标题,2(用于删除单元格

使用下面的代码,我第一次只能删除一个单元格,如果我试图删除另一个无法删除的单元格,。。我收到json验证错误,为什么?。。我想随时删除选定的单元格

这是我的第一个JSON API响应:用于将图像和标题添加到集合视图。。。对于该相应的pic_id,我需要移除小区。。在我的第二个JSON API 中

{
"jsonrpc": "2.0",
"result": {
"image": {
"image": "1616588156.jpg",
"image_title": "City",
"user_id": 2,
"updated_at": "2021-03-24 17:45:56",
"created_at": "2021-03-24 17:45:56",
"pic_id": 14
},
"message": "Image uploaded Successfully!"
}
}

上述JSON服务的代码,用于将图像和标题添加到collectionview。。在这里,我将pic_id像这样保存在UserDefaultsUserDefaults.standard.set(picId, forKey: "pic_id")

fileprivate func postServiceCall(){

if titleTextfield.text?.trim() == ""{
return self.view.makeToast("please add service title")
}
let parameters = ["image_title" : titleTextfield.text?.trim() ?? ""]

APIReqeustManager.sharedInstance.uploadMultipartFormData(param: parameters, url: CommonUrl.edit_profile_images, image: imageProfile, fileName: "image", vc: self, isHeaderNeeded: true) {(responseData) in
print("edit profile result (responseData)")
if let result = responseData.dict?["result"] as? NSDictionary{
let success = result["status"] as? [String : Any]
let message = success?["message"] as? String
if message == "Success"{
let image = result["image"] as? [String : Any]
let picId = image?["id"]
UserDefaults.standard.set(picId, forKey: "pic_id")// here i am saving pic_id
self.arrImageItems.append(ImageItemModel(title: self.titleTextfield.text, imgTitle: self.imageProfile))
self.collectionView.reloadData()
}
else{
self.view.makeToast(CommonMessages.somethingWentWrong)
}
}
}
}

collectionview中删除单元格的JSON服务代码

@objc func deleteService(sender:UIButton) {
let picId = UserDefaults.standard.string(forKey: "pic_id")
print("selected picid (picId)")
let param = ["pic_id" : picId]
APIReqeustManager.sharedInstance.serviceCall(param: param as [String : Any], method: .post, loaderNeed: false, loadingButton: sender as! TransitionButton, needViewHideShowAfterLoading: nil, vc: self,?url: CommonUrl.edit_profile_images_remove, isTokenNeeded: true, isErrorAlertNeeded: true, isSuccessAlertNeeded: false, actionErrorOrSuccess: nil, fromLoginPageCallBack: nil) { [weak self] (resp) in
if let code = ((resp.dict?["result"] as? [String : Any])){
print("total result: (code)")
let success = code["status"] as? [String : Any]
let message = success?["message"] as? String
if message == "Success"{
let selectedIndex = sender.tag
self?.arrImageItems.remove(at: selectedIndex)
self?.collectionView.reloadData()                    }
}else{
self?.view.makeToast(CommonMessages.somethingWentWrong)
}
}
}

这是collectionview cellForItemAt代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as! ImageCollectionViewCell
cell.imgView.image = arrImageItems[indexPath.item].profileImage
cell.lblTitle.text = arrImageItems[indexPath.row].title
cell.deleteButton.tag = indexPath.row
cell.deleteButton.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)
return cell
}

这是ImageItemModel

class ImageItemModel{
var title: String?
var profileImage: UIImage?
var pic_id: String?
init(title: String?, imgTitle: UIImage?, pic_id: String?) {
self.title = title
self.profileImage = imgTitle
self.pic_id = pic_id
}
}

有了上面的代码,我只能第一次删除一个单元格。。如果我试图再删除一个单元格,那么我会收到JSON验证错误。。。如何始终删除所选单元格。。请帮助编码

您在用户默认值中使用相同的键存储图片,这是错误的。它将只存储循环的最后一项。您应该在postServiceCall方法中的模型中添加picID,如

self.arrImageItems.append(ImageItemModel(title: self.titleTextfield.text, imgTitle: self.imageProfile, picID: picId))

然后在deleteService方法中,您需要获得类似的picID

let picId = UserDefaults.standard.string(forKey: "pic_id")//Replace this with this one
let picId = arrImageItems[sender.tag]. picID

现在一切都会好起来的。

最新更新