2015-10-25 20 views
1

我有一個UICollectionView,一切工作正常,但是,有一件事我無法處理(我不知道如何),我有一個單元集合,以查看用戶需要向下滾動或向上滾動的所有單元格。UICollectionView和Selected Cell在滾動時失去選擇

enter image description here

當用戶選擇單元格,內容改變爲「紅」,紅色是「選擇的」細胞,黑色是「未選擇的」細胞或正常狀態。

當選定單元格落在導航欄或TabBar後面時,單元格會丟失「紅色」並再次變黑,如「未選中」。

當uicollectionview滾動時,如何在細胞落後時保持「紅色」?

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) 

    let icon = cell!.viewWithTag(10) as? UIImageView 
    icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 
    icon!.tintColor = UIColor.redColor() 

    let label = cell!.viewWithTag(100) as? UILabel 
    label?.textColor = UIColor.redColor() 
    //print("Seleccionado") 

} 

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) 

    cell!.contentView.backgroundColor = nil 
    cell!.contentView.layer.borderColor = nil 

    let icon = cell!.viewWithTag(10) as? UIImageView 
    icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 
    icon!.tintColor = UIColor.blackColor() 

    let label = cell!.viewWithTag(100) as? UILabel 
    label?.textColor = UIColor.lightGrayColor() 
    //print("Unselect") 
} 

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! ObjectosCollectionViewCell 

    cell.objectoNameLabel.text = objectosData[indexPath.row] 
    cell.objectoNameLabel.textColor = UIColor.lightGrayColor() 
    cell.objectoImageView.image = UIImage(named:objectosImage[indexPath.row]) 

    return cell 
} 

謝謝

回答

2

你需要做你的邏輯一些修改;

//CollectionViewCell Custom Class 
import UIKit 

class CollectionViewCell: UICollectionViewCell { 

    override var selected: Bool { 
     get { 
      return super.selected; 
     } 

     set { 
      if (super.selected != newValue) { 
       super.selected = newValue 

       let icon = self.viewWithTag(10) as? UIImageView 
       icon?.image = icon?.image!.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 

       let label = self.viewWithTag(100) as? UILabel 

       if (newValue == true) { 
        icon?.tintColor = UIColor.redColor() 
        label?.textColor = UIColor.redColor() 
       } else { 
        icon?.tintColor = UIColor.blackColor() 
        label?.textColor = UIColor.lightGrayColor() 
       } 
      } 
     } 
    } //P.E. 

} 

然後;

//Define a class variable in your viewController 
var cellStatus:NSMutableDictionary = NSMutableDictionary(); 

//Collection view delegate methods 
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    var cell:CollectionViewCell? = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as? CollectionViewCell; 

    cell!.selected = (cellStatus[indexPath.row] as? Bool) ?? false; 

    return cell!; 
} 

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    //Updating cell status 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) 
    cell?.selected = true; 

    //Updating dic 
    self.cellStatus[indexPath.row] = true; 
} 

func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { 
    //Updating cell status 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) 
    cell?.selected = false; 

    //Updating dic 
    self.cellStatus[indexPath.row] = false; 
} 

注:改變圖像圖標顏色的做法是不好的。它佔用了太多的處理能力,可能會掛起滾動。每個狀態應該使用兩個單獨的圖像。

+0

謝謝,我喜歡這個解決方案,我試過了,但是一切都會再次變回黑色 – Asinox

+0

你能顯示cellForItemAtIndexPath的代碼嗎? – Shoaib

+0

當然,編輯,添加 – Asinox

0

這發生在我身上用的UITableView,好像你向上滾動,每次上下呈現元素越多,小區再次呈現。所以我遇到的解決方案是創建一個名爲selecteditems的數組,並且每次用戶選擇該單元格時,都將該單元格的索引保存在數組中。 這樣

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
let cell = collectionView.cellForItemAtIndexPath(indexPath) 

let icon = cell!.viewWithTag(10) as? UIImageView 
icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 
icon!.tintColor = UIColor.redColor() 

let label = cell!.viewWithTag(100) as? UILabel 
label?.textColor = UIColor.redColor() 

//Save in array 
selecteditems.append(indexPath.row) 

}

override func collectionView(collectionView: UICollectionView, didDeselectItemAtIndexPath indexPath: NSIndexPath) { 
    let cell = collectionView.cellForItemAtIndexPath(indexPath) 

    cell!.contentView.backgroundColor = nil 
    cell!.contentView.layer.borderColor = nil 

    let icon = cell!.viewWithTag(10) as? UIImageView 
    icon!.image = icon!.image! .imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate) 
    icon!.tintColor = UIColor.blackColor() 

    let label = cell!.viewWithTag(100) as? UILabel 
    label?.textColor = UIColor.lightGrayColor() 


    if selecteditems.contains(indexPath.row){ 
     // place your code to be red 
    } 

} 
+0

謝謝,我試過這個,但是沒有什麼變化,確定有我的元素陣列,但是一切都會再次變黑 – Asinox