I’m building an app with iPod-like controls (play, pause, etc). The app has tableView with track names in each cell. I have a MainViewController with a UITableView and a custom UITableViewCell class. The player controls exist in the MainViewController.
I also have a play/pause button in each cell. I’ve successfully set up NSNotifications to post a notification when the play button is pressed in the cell, so the track info is sent to the observer and responder method in MainViewController and the player controls (driven by MPMoviePlayerController) is initiated.
This works, but once the track is playing, I can’t figure out how to make the play/pause button in the cell “mirror” the state of the play/pause button in my iPod controls. I don’t think adding a NSNotification observer in every cell is the correct way of doing this.
Additional info: The play/pause button image in the MainViewController player is determined by a method that fires every half second and checks the playbackstate of the MPMoviePlayerController. If playing, the player button set’s the play image. If paused, it set’s the pause image. I’m thinking the implementation to also set the tableCell play/pause button will go here.
Edit This method is called whenever the playback state changes (via NSNotification), within my UITableViewController. (playButton is the play/pause button in the player controls, not the play/pause in the table cell in which I’m trying to update according to playback state).
- (void) updateViewForPlayerState
{
// Change playButton image depending on playback state
[playButton setImage:((moviePlayer.playbackState == MPMoviePlaybackStatePlaying) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
}
Use
[tableView reloadData]when your state changed and on providing a cell incellForRowAtIndexPath:set the button according to the state of your data (i.e whether the song that cell refers to is playing or not).Also instead of notifications you could use delegation which is the usual approach. But notifications also work especially if you need multiple observers.