我有多個UICollectionViewCells
。當用戶點擊一個特定的單元格時,我希望我的應用程序更改被觸摸的單元格的背景圖像。如何更改已被點擊的UICollectionViewCell的背景圖像?
我的方法是專注於didSelectItemAtIndexPath
方法。當一個單元格被觸摸時,這個方法將被調用。
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
var cell: UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) as! UICollectionViewCell
cell.backgroundView = UIImageView(image: UIImage(named: "myImage"))
}
但是,我不能得到它的工作,但我不知道爲什麼。該問題可能與indexPath
有關,即不返回正確的單元格值。我嘗試使用indexPath.row
,這實際上返回一個Int
號碼的單元格。
更重要的是,我還用var
創建了一個新的UICollectionViewCell
,但此單元格已存在。
爲什麼不更新其背景圖像?如何更改用戶已觸摸的UICollectionViewCell
的背景圖像?
您可以使用'cellForItemAtIndexPath'來獲取單元而不是使另一個出列? –
'cellForItemAtIndexPath'是一種用於將單元放入「UICollectionView」中的方法。當你選擇一個單元格時它不會被調用。 – Cesare
對不起,我不清楚。使用'cellForItemAtIndexPath'作爲獲取單元格的方法。不是完全確定swift語法,而是'collectionView.cellForItemAtIndexPath(indexPath)'。 –