0

I have a collectionViewCell with a button I want to add a function to (like/unlike function) If user has liked the post the post tag should be set to 1,(color changed to blue) if user unlike the button tag it reset back to 0.(color changed to white) The problem I currently have is while scrolling random buttons within my collectionView button colors are being change, even when not selected.

 class customCollectionViewCell: UICollectionViewCell {
        @IBAction func followBtn(sender: UIButton) {
            if (sender.tag == 0){
                likePost{(msg)
                in
                    if (msg == 0){
                        self.likeBtn.tintColor = UIColor.blueColor()
                        self.likeBtn.tag = 1
                    }
                }
            }
            else{
              //Unfollow function
               self.likeBtn.tintColor = UIColor.whiteColor()
                self.likeBtn.tag = 0
            }
        }
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("posterCell", forIndexPath: indexPath)
                as! customCollectionViewCell

     cell.likeBtn.tag = 0
            if (cell.liketBtn.tag == 0){
            cell.followArtistBtn.tintColor = UIColor.whiteColor()

            }
            if (cell.likeBtn.tag == 1){
           cell.followArtistBtn.tintColor = UIColor.blueColor()
            }
}
et moi
  • 127
  • 1
  • 1
  • 9

4 Answers4

1

This is due to cell reuse. You'll have to set a default value in your cellForItemAtIndexPath and then change the color of the button accordingly to whether or not it should be liked based on your data.

pbush25
  • 5,228
  • 2
  • 26
  • 35
  • I've tried moving the condition statement to `cellForItemAtIndexPath`, i still receive the same results – et moi Nov 17 '15 at 05:47
  • But the important part is **did you set a default value for the buttons before trying to change their state**? – pbush25 Nov 17 '15 at 05:48
  • Just did that, result are different but not much different, this removes the tintColor on the previously selected button on scroll – et moi Nov 17 '15 at 05:54
1

This issue happening due to you are not changing datasource according to the selection of button. Here is some basic steps to do

var users = NSMutableArray()
let user1 = ["name" : "Sample User 1", "age": 21, "isFav": 0];
let user2 = ["name" : "Sample User 2", "age": 21, "isFav": 0];
users = [user1, user2]

@IBAction func followBtn(sender: UIButton) {
    var dicUser =  NSMutableDictionary(dictionary: users [sender.tag])
    dicUser["isFav"] = (dicUser.valueForKey("isFav") as! Bool) ? 0 : 1;
    users.replaceObjectAtIndex(sender.tag, withObject: dicUser)
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
   let cell = collectionView.dequeueReusableCellWithReuseIdentifier("posterCell", forIndexPath: indexPath)
                as! customCollectionViewCell
   cell.likeBtn.tag = indexPath.row
   cell.followArtistBtn.tintColor =(users[sender.tag]].valueForKey("isFav") as! Bool) ? UIColor.whiteColor() : UIColor.blueColor();
}
Narayana Rao Routhu
  • 6,303
  • 27
  • 42
0

this is happening because collection view reuse same cell every time. for this u store your tags in an array according to every cell position... and when user scrolls and cell loads... check that tag is 1 or 0 for that button or not... and change ur button color according to that

Divyanshu Sharma
  • 551
  • 2
  • 12
  • Im not too sure how i can store the index of the current button within the collection view and the array of tag. Could you provide some code please? – et moi Nov 17 '15 at 05:49
  • When user click on button you can get cell by getting its superView because cell is superView of button. Like this yourCell *cell = [button superView] superView]…. Till you get your cell when u get cell then you can get indexPath for that cell and can add tag for that indexPath in array and leave o for other IndexPaths – Divyanshu Sharma Nov 17 '15 at 05:54
0

This is because of cell reuse. You have to set the default values to cell in cellforrowatindexpath and then assign the value to controls.

If you are used to make small number of rows then you can dequeue cells for every row without reusing the cell. But its not the best practice for large number of rows.

shlok
  • 113
  • 8