2017-11-11 8 views
0

UICollectionViewCell에 UIButton이 있습니다. 이 셀에서 json id를 얻고 싶습니다. UICollectionViewController의 json 데이터를 완벽하게 파싱합니다. UICollectionViewCell에서 UIButton을 클릭 할 때 항상 end id에서 문제가 발생합니다. UICollectionViewController, UICollectionViewDelegateFlowLayout {스위프트 : UIButton CollectionView 셀에서 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 내 코드

입니다

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

추가하십시오 코드를 제대로 StoreCollectionViewCell 클래스에서 데이터 소스 방식으로 다음

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 } 

와 ID를 지정하고 또한 collectionview 방법을 추가해야합니다. – ivarun

답변

2

때문에 당신의 ProductStoreId.store_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

관련 문제