0

I want to hide the label in a cell that was tapped and instead show an image. But I want to do this only if a cell with a certain index has already been set to the imageView. What is the best way to address the cells and store if they are set to imageView or not? How do I use the prepareForReuse method?

This is the way I do it until now, but as the cells are reused. The image is shown in other cells at scrolling.

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

    println("user tapped on door number \(indexPath.row)")

    let cell = collectionView.cellForItemAtIndexPath(indexPath) as! MyCollectionViewCell

    if (cell.myLabel.text == "1") {
        one = true

        if(seven = true) {

            if (cell.myLabel.hidden) {
                cell.myLabel.hidden = false
                cell.MyImageView.image = nil

            }
            else {
                cell.myLabel.hidden = true
                cell.MyImageView.image = UIImage(named:"1")!
            }

         }
    }
maidi
  • 3,219
  • 6
  • 27
  • 55

2 Answers2

0

You didn't say if your collection view has exactly 7 cells or if it can have "N" (e.g. 100) cells in the collection, so if this were my problem and I had to solve it, I would make the state of your "seven" cell a property of the class (e.g. "var sevenState : Bool") and then I could display the button or image of other cells depending on what sevenState is.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • I have an exact number of cells but each is "connected" to another so you can just tap them in a predefined order – maidi Nov 28 '15 at 14:41
  • How many cells do you have, exactly? If you need to keep state information around for certain cells, then you'd need to have some underlying data structure (whether that Bool property, or an array, or something) that keeps track of what the state of various cells are. – Michael Dautermann Nov 28 '15 at 14:57
0

In my app I have to configure a UICollectionReusableView based on the index path, if the indexPath has a particular value then I send an array which is used to set labels and images.

I use a function in the custom UICollectionReusableView, if I call it with an array it populates the labels and images and if I call it with nil it resets these.

func collectionView(collectionView: UICollectionView!, viewForSupplementaryElementOfKind kind: String!, atIndexPath indexPath: NSIndexPath!) -> UICollectionReusableView!  {
     .... [logic around selecting index path based on data returned]
     .... 
   if filteredEvents != nil{
            reusableView.populateCalendarDayDates(sortedEvents)
   }else{
            reusableView.populateCalendarDayDates(nil)
   }

In the function in the custom UICollectionReusableView I reset labels back to default values before possibly updating them :

func populateCalendarDayDates(arrayEvents: NSArray?){
    let firstDayTag = tagStartDay()
    var dayDate = 1

    for var y = 1; y < 43; y++ {
        let label = self.viewWithTag(y) as! BGSCalendarMonthLabel
        label.delegate = callingCVC
        label.backgroundColor = UIColor.clearColor()
        label.textColor = UIColor.whiteColor()

        label.text = ""

You can get the same effect, and it is probably a bit more readable, by moving this code to prepareForReuse in the custom UICollectionReusableView :

override func prepareForReuse() {
    super.prepareForReuse()
    for var y = 1; y < 43; y++ {
        let label = self.viewWithTag(y) as! BGSCalendarMonthLabel
        label.backgroundColor = UIColor.clearColor()
        label.textColor = UIColor.whiteColor()

        label.text = ""
    }

}

Hope that helps.

Peter Todd
  • 8,561
  • 3
  • 32
  • 38