2014-07-22 63 views
2

我正在嘗試使用由上述UIImageViewUIView組成的自定義單元格執行簡單的UICollectionViewUICollectionView和選定的UICollectionViewCell

當未選擇的單元,在電池的頂部的UIView具有是backgroundColor屬性設置爲UIColor(red: 1, green: 1, blue: 1, alpha: 0.5)

我對選擇有問題。 collectionView:didSelectItemAtIndexPath:方法被調用,但當我更改前面描述的UIView時,什麼都不會發生。

這裏的代碼我collectionView

class TestCollectionViewController: UICollectionViewController 
{ 
    var items = [1, 2, 3] 

    let cellId = "Cell" 

    override func viewDidLoad() 
    { 
     self.collectionView.allowsMultipleSelection = true 
     self.collectionView.delaysContentTouches = false 
    } 

    override func collectionView(collectionView: UICollectionView!, numberOfItemsInSection section: Int) -> Int 
    { 
     return items.count 
    } 

    override func collectionView(collectionView: UICollectionView!, cellForItemAtIndexPath indexPath: NSIndexPath!) -> UICollectionViewCell! 
    { 
     var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, 
      forIndexPath: indexPath) as TestingCollectionViewCell 

     let item = items[indexPath.row] 

     cell.imageView.image = UIImage(named: "img") 
     cell.overlayView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5) 

     return cell 
    } 

    override func collectionView(collectionView: UICollectionView!, didSelectItemAtIndexPath indexPath: NSIndexPath!) 
    { 
     var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, 
      forIndexPath: indexPath) as TestingCollectionViewCell 

     cell.overlayView.backgroundColor = UIColor.clearColor() 
    } 

    override func collectionView(collectionView: UICollectionView!, didDeselectItemAtIndexPath indexPath: NSIndexPath!) 
    { 
     var cell = collectionView.dequeueReusableCellWithReuseIdentifier(cellId, 
      forIndexPath: indexPath) as TestingCollectionViewCell 

     cell.overlayView.backgroundColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.5) 
    } 
} 

如果這個片段是不夠的,這裏是my project

回答

10

你不應該出隊在didSelectItemAtIndexPath一個新的小區(或didDeselect要麼) - - 這是創建(或重新使用)一個新的單元格,沒有得到你選擇的單元格。使用此相反,

var cell = collectionView.cellForItemAtIndexPath(indexPath) 
+0

非常感謝你爲這個的元素,已經掙扎了一段時間!但是,我的新圖像被繪製在UICollectionView的中間,而不是在單擊的單元格中。任何想法爲什麼? – kakubei

+0

@kakubei你問什麼新形象?你問關於imageView.image或overlayView? – rdelmar

+0

我創建了一個包含該圖像的imageView和一個reloadData圖像移動到另一個單元。我無法弄清楚爲什麼。 – kakubei

1

對我來說,下面的代碼更好地工作(rdelmar的代碼有時會崩潰,這可能是一個錯誤的斯威夫特?):

(collectionView.cellForItemAtIndexPath(indexPath) as myCollectionViewCellType).uiBackground?.backgroundColor = UIColor.blackColor() 

編輯:我忘了提:我的collectionView-Items是基於他們自己的類別來投放的。 uiBackground是類

0
 func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
     { 

    //saving index path of the image loaded from imageproperty_array    
     selected_imagepath = imageproperty_array[indexPath.row] as String 

    //saving index of the selected cell item as Int    
     selected_imageindex = indexPath.row 

     } 
1

雨燕3.0

let cell = collectionView.cellForItem(at: indexPath) 
相關問題