2015-06-04 22 views
1
  • 斯威夫特,沒有故事板,UICollectionView從XIB文件

我正在非常簡單的代碼更新單元格的背景顏色,但更多的則一個細胞更新。UICollectionView didSelectItemAtIndexPath影響多個單元格 - 迅速

 override func viewDidLoad(){ 
    super.viewDidLoad() 
    // getting images from library 
    images = PHAsset.fetchAssetsWithMediaType(.Image, options: nil) 

    collectionView.allowsMultipleSelection = false 
    var nipName2 = UINib(nibName: "CollectionViewCell", bundle:nil) 
    collectionView.registerNib(nipName2, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "HeaderCell") 
    var nipName = UINib(nibName: "MyViewCell", bundle:nil) 
    collectionView.registerNib(nipName, forCellWithReuseIdentifier: "Cell") 
} 

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

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

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyViewCell 
    cell.frame.size.width = 60 
    return cell 
} 

func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView{ 
    switch kind 
    { 
    case UICollectionElementKindSectionHeader: 
     let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "HeaderCell", forIndexPath: indexPath) as! CollectionViewCell 
     return headerView 
    default: 
     assert(false, "Unexpected element kind") 
    } 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) 
{ 
    var cell = collectionView.cellForItemAtIndexPath(indexPath) 
    cell?.backgroundColor = UIColor.redColor() 
} 

我有大約300個不同的細胞。我想更新第三個,但隨機更改其他單元的背景。

+0

我建議覆蓋你的'UICollectionViewCell'的'setSelected:animated:'。 – Larme

+0

您是否允許在'UICollectionView'中進行多項選擇? –

+0

- 「collectionView.multipleTouchEnabled = false」不起作用,如果這是你的意思。 - 「collectionView.allowsMultipleSelection = false」也不起作用。 – fatihyildizhan

回答

2

因此集合和表視圖具有強烈的視圖重用概念。這樣可以獲得較大的性能提升,因爲它不需要同時在內存中保存300個單元。

當您在某些單元格上設置背景顏色時,這些單元格有可能在滾動時重新使用。由於您沒有明確地設置視圖顯示的背景,它只是使用當前的任何內容。

爲了解決這個問題剛纔設置的顏色請求視圖時:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ 
    // This does not guarantee to be a fresh, new cell, can be reused 
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! MyViewCell 
    cell.frame.size.width = 60 
    // Explicitly set the background color: 
    cell.backgroundColor = .whiteColor() // or whatever your default color is 
    return cell 
} 

現在很明顯,這將導致更多的副作用。假設您將單元格的背景顏色更改爲紅色並滾動。當你回來時,你現在將它重新設爲白色。這就是說,你需要跟蹤哪些單元格(可能通過存儲它們的索引)被選中並適當地設置它們的顏色。

+0

謝謝,它工作 – fatihyildizhan

相關問題