簡體   English   中英

如何根據 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 您可以將標簽添加到您的按鈕,其標簽值為indexPath.row中的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 // added line
cell.deleteButton.addTarget(self, action: #selector(deleteService(sender:)), for: UIControl.Event.touchUpInside)

return cell

然后在deleteService function 你可以達到索引

@objc func deleteService(sender:UIButton) { 
   let selectedIndex = sender.tag
 ......

}

筆記

您使用let i = sender.tag但在單元格中您沒有為按鈕添加標簽,因此sender.tag0這就是為什么總是刪除數組的第一個元素的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM