2017-11-11 104 views
0

我在UICollectionViewCell中有一個UIButton。想從這個單元格獲得json id。我完美地解析了UICollectionViewController中的json數據。當我點擊UICollectionViewCell中的UIButton時,我總是收到end id的問題。Swift:如何從UIButton CollectionView cell調用json id

這裏是我的代碼

的Json

{ 
    "error": false, 
    "product_sellers": [ 
    { 
     "id": 5, 
     "store_name": "ahmedstore", 
     "photo": "user_12-06-2017-12-32-56.png", 
     "is_favorite": 0 
    }, 
    { 
     "id": 122, 
     "store_name": "aitldev6", 
     "photo": null, 
     "is_favorite": 0 
    }, 
    { 
     "id": 123, 
     "store_name": "aitlmanager", 
     "photo": "user_2017-08-31-10-06-am (16).jpg", 
     "is_favorite": 0 
    }, 
    { 
     "id": 135, 
     "store_name": "ajanta library", 
     "photo": "user_2017-08-19-7-16-am23931.png", 
     "is_favorite": 0 
    }, 

CollectionViewController

類ProductStoreCollectionViewController:UICollectionViewController,UICollectionViewDelegateFlowLayout {

override func viewDidLoad() { 

    super.viewDidLoad() 


    let url = URL(string: "..........") 


    URLSession.shared.dataTask(with:url!) { (urlContent, response, error) in 

     if error != nil { 
      print(error ?? 0) 
     } 
     else { 
      do { 

       let items = json["product_sellers"] as? [[String: Any]] ?? [] 

       self.arrStore = [Store]() 

       for item in items { 


        let oStore = Store() 

        oStore.id = item["id"] as? Int 
        oStore.image = item["photo"] as? String 
        oStore.store_name = item["store_name"] as? String 
        oStore.is_favorite = item["is_favorite"] as? Int 

        ProductStoreId.store_id = oStore.id! 

        self.arrStore.append(oStore) 

       } 

      } catch let error as NSError { 
       print(error) 
      } 

      DispatchQueue.main.async(execute: { 

       self.collectionView?.reloadData() 
      }) 

     } 



    }) 

}

UICollectionViewCell

class StoreCollectionViewCell: BaseCell{ 



    let heartBtn: UIButton = { 

     let btn = UIButton() 
     btn.setImage(UIImage(named: "heart_white_copy"), for: .normal) 

     btn.translatesAutoresizingMaskIntoConstraints = false 
     return btn 
    }() 

    var sellerId2: Int = 1 


    func heartBtnClick(){ 

     print("box Btn click") 


     self.sellerId2 = ProductStoreId.store_id 


     print("add to favoriteStore\(self.sellerId2)") 


    } 


    override func setupCell() { 

     heartBtn.addTarget(self, action: #selector(heartBtnClick), for: .touchUpInside) 
     addSubview(heartBtn) 




    } 

} 
+0

請添加代碼正確地分配ID

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // Your code let store = self.arrStore[indexPath.row] // Please correct it if not cell.sellerId2 = store.id } 

,然後在StoreCollectionViewCell類,也可以添加的CollectionView方法。 – ivarun

回答

2

這是因爲你的ProductStoreId.store_id有,當你分析它存儲的最後一個ID。

您需要在您的數據源方法

func heartBtnClick(){ 
    print("box Btn click") 
    //We don't need the below line 
    //self.sellerId2 = ProductStoreId.store_id 
    print("add to favoriteStore\(self.sellerId2)") 
} 
+0

謝謝你的回答。我的問題是解決 – Minzu

+0

你能幫我回答這個問題嗎?我想通過點擊按鈕來檢測tableview單元格中的一個按鈕。這裏是鏈接https://stackoverflow.com/questions/48970198/how-to-detect-one-button-in-tableview-cell?noredirect=1#comment84948065_48970198 – Minzu

相關問題