如何根据 swift 中的 JSON 参数值删除带按钮的 CollectionView 单元格
How to delete CollectionView cell with button according to JSON parameter value in swift
我无法根据 JSON 参数值删除 CollectionView 单元格。我得到正确的 pic_id
值。但是如果我在这里删除那个单元格,collectionview 的第一个单元格总是被删除。 .
如果我删除任何单元格,它总是从头到尾删除
JSON 请求:
{
"jsonrpc": "2.0",
"params": {
"pic_id": "84"
}
}
JSON 回复:
{
"jsonrpc": "2.0",
"result": {
"status": {
"code": "-36739",
"message": "Success",
"meaning": "Images deleted Successfully"
}
}
}
代码:使用以下代码..collectionview 单元格按顺序添加..但是如果我删除最后一个单元格,它总是删除第一个单元格..我哪里错了..请帮忙编写代码
extension EditProfileImageViewController : UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrImageItems.count
}
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.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
@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 i = sender.tag
self?.arrImageItems.remove(at: i)
self?.collectionView.reloadData() }
}else{
self?.view.makeToast(CommonMessages.somethingWentWrong)
}
}
}
我需要删除选定的 pic_id
单元格..但它总是删除第一个单元格..请帮忙
我更喜欢在这种情况下使用 tag
。您可以向按钮添加标签,标签值为 cellForItemAt
中的 indexPath.row
,例如:
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 // added line
cell.deleteButton.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
然后在 deleteService
函数中你可以达到索引
@objc func deleteService(sender:UIButton) {
let selectedIndex = sender.tag
......
}
NOTE
你使用 let i = sender.tag
但在单元格中你没有给你的按钮添加标签所以 sender.tag
会被 default.This 变成 0
这就是数组的第一个元素的原因总是被删除。
我无法根据 JSON 参数值删除 CollectionView 单元格。我得到正确的 pic_id
值。但是如果我在这里删除那个单元格,collectionview 的第一个单元格总是被删除。 .
如果我删除任何单元格,它总是从头到尾删除
JSON 请求:
{
"jsonrpc": "2.0",
"params": {
"pic_id": "84"
}
}
JSON 回复:
{
"jsonrpc": "2.0",
"result": {
"status": {
"code": "-36739",
"message": "Success",
"meaning": "Images deleted Successfully"
}
}
}
代码:使用以下代码..collectionview 单元格按顺序添加..但是如果我删除最后一个单元格,它总是删除第一个单元格..我哪里错了..请帮忙编写代码
extension EditProfileImageViewController : UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return arrImageItems.count
}
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.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
@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 i = sender.tag
self?.arrImageItems.remove(at: i)
self?.collectionView.reloadData() }
}else{
self?.view.makeToast(CommonMessages.somethingWentWrong)
}
}
}
我需要删除选定的 pic_id
单元格..但它总是删除第一个单元格..请帮忙
我更喜欢在这种情况下使用 tag
。您可以向按钮添加标签,标签值为 cellForItemAt
中的 indexPath.row
,例如:
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 // added line
cell.deleteButton.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)
return cell
}
然后在 deleteService
函数中你可以达到索引
@objc func deleteService(sender:UIButton) {
let selectedIndex = sender.tag
......
}
NOTE
你使用 let i = sender.tag
但在单元格中你没有给你的按钮添加标签所以 sender.tag
会被 default.This 变成 0
这就是数组的第一个元素的原因总是被删除。