'Update Custom Cell UIButton title when NSNotification is posted
I have a UITableViewController with a bunch of custom cells that have a playButton in them. In cellForRowAtIndexPath, I assign a tag that is is equal to the indexPath of the button, so that when the playButton is pressed, I set the title of the button to "Stop". It changes to "Stop", as it should, and when I press "Stop", it changes back to "Play".
Where I'm having difficulty is when my sound stops playing on its own, absent user intervention. I set up an observer to listen for the MP3 player being done. I added an observer in viewDidLoad of MyTableViewController:
Here are the variables I use to facilitate changing the title of the playButton in my cells:
// Variables to facilitate changing playButton title
var indexPathOfPlayButton = Int()
var isPlaying: Bool = false
In viewDidLoad of MyTableViewController, I add this observer:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "resetPlayButton", name: resetPlayButtonNotification, object: nil)
Here's my playMP3 method on MyTableViewController:
func playMP3(sender: AnyObject) {
if isPlaying == false {
isPlaying = true
// This gets the indexPath of the button that sent the playMP3 request
let indexPath = sender.tag
sender.setTitle("Stop", forState: UIControlState.Normal)
// This sets the indexPath of the playButton that we'll redraw the button when it receives a notification?
indexPathOfPlayButton = indexPath
if resultsSearchController.active {
let soundToPlay = self.filteredSounds[indexPath]
let soundFilename = soundToPlay.soundFilename as String
mp3Player = MP3Player(fileName: soundFilename)
mp3Player.play()
} else {
let soundToPlay = self.unfilteredSounds[indexPath]
let soundFilename = soundToPlay.soundFilename as String
mp3Player = MP3Player(fileName: soundFilename)
mp3Player.play()
}
}
else if isPlaying == true {
isPlaying = false
sender.setTitle("Play", forState: UIControlState.Normal)
mp3Player.stop()
}
}
In my MP3Player class, this is the delegate method I use post a notification that it's done:
func audioPlayerDidFinishPlaying(player: AVAudioPlayer, successfully flag: Bool) {
if currentTrackIndex == tracks.count - 1 {
print("end of playlist reached")
player.stop()
NSNotificationCenter.defaultCenter().postNotificationName(resetPlayButtonNotification, object: self)
}
else if flag == true {
print("advance to next track")
nextSong(true)
}
}
Lastly, this is the method on MyTableViewController that gets called when a notification is posted:
func resetPlayButton() {
print("resetPlayButtonCalled")
// TODO: How do I get a hold of the button and change the title from outside playMP3?
}
Solution 1:[1]
You have a lot of code, which is not related to the problem your mentioned above. If I understood your problem is that you don't know how to update button in your observer selector resetPlayButton(). The button is in the tableview cell and you have stored the index in indexPathOfPlayButton.
You can get the cell like:
let cell: YourCellClass = tableView.cellForIndexPath(NSIndexPath(forItem: indexPathOfPlayButton, inSection: 0))
And after you get the cell, you can update its output.
Another way is to call reloadData()
Solution 2:[2]
For me it was using the main thread to post the notification:
DispatchQueue.main.async {
NotificationCenter.default.post(name: Notification.Name("identifier"), object: true, userInfo: ["text": "Purchased"])
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | katleta3000 |
| Solution 2 | Codetard |
