I have a table view full of songs, each cell has a Play button. Once you press the Play button the song plays and the Play button in the individual cell turns to a Stop button. Now this cell was done using View Tags, so when you scroll down further and as cells are reused, random cells that come into the view have a Stop button even though they were never selected. What is the best way to prevent this reuse from happening? Should I refactor my code into a custom UITableViewCell class and prevent reuse on the button? Or is there a quicker work around here?
Asked
Active
Viewed 100 times
-1
-
have you tried meda's answer? Do you still get the re-using breaking your cell? – Sana Dec 24 '15 at 00:52
-
instead of disabling cell reuse you should fix your code. You didn't post it, so we can't help you. But one advice: Working with view's tag is a poor solution — nearly always. Especially with cells. – vikingosegundo Dec 26 '15 at 17:33
1 Answers
1
You should remember playing state for song and update button status when reusing the cell (the same way you're updating other properties like title, artist etc).
For example, you can save playing song index in controller private variable (initially set it to -1
) and compare it to indexPath.row
when reusing the cell.

Alex Skalozub
- 2,511
- 16
- 15
-
It's better to store this somewhere in the data model, but your basic premise is spot-on. – Avi Dec 24 '15 at 06:04
-
Not entirely sure what you mean here with comparing the private variable. @Avi It's a bit tricky in this situation, we are calling our tableview data down from Parse so there isn't a local data model to store the playing status on. – pennyloaf Dec 26 '15 at 16:51
-
@pennyloaf: using parse is no explanation for not having a local model. – vikingosegundo Dec 26 '15 at 18:02
-
The "data model" doesn't have to be some roped-off set of classes. The data model includes any data structure that holds data about the model. This includes whether a song is playing. It doesn't matter that this data is transient. In contrast, the view simply checks the model (which can be a single instance variable in a relevant class) to see which, if any, song is playing. – Avi Dec 27 '15 at 03:16