Omid Abutorab
Omid Abutorab

Reputation: 43

change the objects of just one collectionview cell

What I'm trying to do is when I tap the button in a cell, that button in that cell becomes invisible. The problem is when I tap the button, it becomes invisible, but when I scroll the collection view the hidden button goes from one to the other. For example, I tap the second one it hides but when I scroll I see that the 7th becomes hidden. Every time I scroll the hidden button change.

This is the code I wrote:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   let cell : CollectionViewCellKharid3 = collectionView.dequeueReusableCell(withReuseIdentifier: "customcell3", for: indexPath) as! CollectionViewCellKharid3

        cell.lblEsmeMahsul.text = mainCats[indexPath.row]

        cell.imgMahsul.af_setImage(withURL: URL(string : (mainadress + "/Opitures/" + mainPicNumbers[indexPath.row]))!, placeholderImage: UIImage(named: "loadings" ))


        cell.btnKharid.addTarget(self, action: #selector(btnColectionviewCellTapped), for : UIControlEvents.touchUpInside)
        cell.btnKharid.tag = indexPath.row

         cell.btnMosbat.addTarget(self, action: #selector(btnMosbatTapped), for : UIControlEvents.touchUpInside)
        cell.btnMosbat.tag = indexPath.row


        cell.configureCell()
        return cell
    }

@objc func btnColectionviewCellTapped(_ sender:UIButton){
    // let indexPath : IndexPath = self.collectionview1.ind

    print(sender.tag)

    sender.isHidden = true
}

@objc func btnMosbatTapped(_ sender:UIButton){
    let index = IndexPath(item: sender.tag , section: 0)
    let cell = self.collectionviewForushVije.cellForItem(at: index) as? CollectionViewCellKharid3

    cell?.lblTedad.text = "22"
    print(sender.tag)
}

enter image description here

Upvotes: 0

Views: 104

Answers (2)

MatthewLuiHK
MatthewLuiHK

Reputation: 701

It's a very common mis-use of UICollectionView(or UITableView). When deal with them, you should alway keep one thing in mind, re-use. The collection/tableview cell will be highly reuse by os when on need. The problem cause in your code is, you assume the one time set of one property in a cell will be persistence, which is wrong. The cell come from dequeue method, can always be a new cell or an existing cell, therefore, any configuration should be apply to a cell should be config again. Think in that way, all view in a cell is "dirty" when it get it from collection view, you should set the property you want before return it back(or have a mechanism to set it later). Therefore, in your case, just set the isHidden property every time you prepare the cell in cellForRow delegate.

Upvotes: 0

rmaddy
rmaddy

Reputation: 318774

Cells get reused. You need to keep track of which cells have been tapped so you can set the proper button state in your cellForItemAt method.

Declare a property in your class:

var beenTapped: Set<Int> = []

Then in btnColectionviewCellTapped add:

beenTapped.insert(sender.tag)

And in cellForItemAt you need:

cell.btnKharid.isHidden = beenTapped.contains(indexPath.item)

You should also replace the use of indexPath.row with indexPath.item. row is for table views. item is for collection views.

Upvotes: 1

Related Questions