-2

I want to add custom subView inside tableViewCell. Basically every time i click on the image inside cell. it will display custom subView with label on it. The link to my project is here

The problem is when i click on the specific cell (0) for example. It displays (0) value but it also display (0) value on indexPath #2. The same goes for when i click on cell (1), it will also display (1) value on indexPath #3. And the pattern goes by (0-2) and (1-3)

I have heard that people are suggesting to use prepareForReuse method, but no luck so far.

I would welcome any ideas and help. Thank you.

1 Answers1

0

You need ro clear the cell as they are deququed

let subview = TagBox(frame: CGRect(x: self.view.frame.width/2, y: self.view.frame.height/2, width: 100, height: 30), boxName: label)
subview.backgroundColor = UIColor.black
subview.tag = 120

cell.contentView.subviews.forEach {
    if $0.tag == 120 { 
        $0.removeFromSuperview()
    } 
}

Or inside the cell

override func prepareForReuse() {
    super.prepareForReuse()
    self.contentView.subviews.forEach {
        if $0.tag == 120 {
            $0.removeFromSuperview()
        }
    }
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87