2017-05-02 59 views
0

有圖像滑塊。我做了一個圖像滑塊,每個都是按鈕。我也改變了按鈕的圖像,現在我不能單擊圖像滑塊中的按鈕來更改UIImageView「imgtoy」。我想單擊按鈕,然後UIImageView將更改爲我的choosetoy數組中的每張照片。任何人都可以幫助我?這裏是我的代碼:如下Swift 3帶按鈕的圖像滑塊無法更改imageview

​​
+0

具有u reloadData在按鈕動作 –

+0

@ShabbirAhmad什麼reloadData。我是swift3 – HappyChan

+0

的初學者,如果你想改變你的集合或表格數據,那麼你必須重新加載按鈕method.like'CollectionViewObj.reloadData()'中的數據。 –

回答

0

更新代碼,

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "foodCollectionViewCell", for: indexPath) as! foodCollectionViewCell 


    cell.imgfood.setImage(imgBackground[indexPath.row],for: UIControlState.normal) 


    cell.imgfood.tag = indexPath.row 

    return cell 
} 

@IBAction func choosetoy(_ sender: UIButton) { 
     imageName = choosetoy[sender.tag] 
     self.imgtoy.image = UIImage(named: imageName) 
} 
+0

這是工作。所以我想知道什麼是.tag?爲什麼標籤可以得到我點擊圖片的數量? – HappyChan