I’ve created a custom UITableViewCell and each cell is passed a custom subclass of AVPlayer object from the UITableViewController. On each cell I have a play button, pause button and loading indicator.
When I play the audio, elements work as required, and change when player state has changed e.g. when playing, pause button appears, play button disappears. When I play audio on a second cell, the first cell knows this, resets it button state, and the second cell does it’s business.
So this functionality works perfectly, the only problem is because the UITableViewCells get reused, when I scroll down to cells below, I start seeing the pause button appear on them. This is because they are the same cells as the ones above (reused) and because my cells are delegates for my custom subclass of AVPlayer, the audio player is sending messages to a cell which isn’t the correct one.
What can I do to make each UITableViewCell a separate delegate object for my AVPlayer?
I solved the problem by making the
UITableViewControllerthe delegate for the audio player. Then saving the “currently playing” cell’sindexPathto a@propertyin theUITableViewController.Then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathI check to see if the indexPath is the same as the “currently playing” cell’sindexPath, if so set the “audio playing” button arrangent, if not then set the default button arrangement.This works better in distinguishing cells as you have a unique identifier
indexPathto compare them with.