ios - Update Custom Cell UIButton title when NSNotification is posted -
i have uitableviewcontroller bunch of custom cells have playbutton in them. in cellforrowatindexpath, assign tag is equal indexpath of button, when playbutton pressed, set title of button "stop". changes "stop", should, , when press "stop", changes "play".
where i'm having difficulty when sound stops playing on own, absent user intervention. set observer listen mp3 player being done. added observer in viewdidload of mytableviewcontroller:
here variables use facilitate changing title of playbutton in cells:
// variables facilitate changing playbutton title var indexpathofplaybutton = int() var isplaying: bool = false in viewdidload of mytableviewcontroller, add observer:
nsnotificationcenter.defaultcenter().addobserver(self, selector: "resetplaybutton", name: resetplaybuttonnotification, object: nil) here's playmp3 method on mytableviewcontroller:
func playmp3(sender: anyobject) { if isplaying == false { isplaying = true // gets indexpath of button sent playmp3 request let indexpath = sender.tag sender.settitle("stop", forstate: uicontrolstate.normal) // sets indexpath of playbutton we'll redraw button when receives notification? indexpathofplaybutton = indexpath if resultssearchcontroller.active { let soundtoplay = self.filteredsounds[indexpath] let soundfilename = soundtoplay.soundfilename string mp3player = mp3player(filename: soundfilename) mp3player.play() } else { let soundtoplay = self.unfilteredsounds[indexpath] let soundfilename = soundtoplay.soundfilename string mp3player = mp3player(filename: soundfilename) mp3player.play() } } else if isplaying == true { isplaying = false sender.settitle("play", forstate: uicontrolstate.normal) mp3player.stop() } } in mp3player class, delegate method use post notification it's done:
func audioplayerdidfinishplaying(player: avaudioplayer, 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 next track") nextsong(true) } } lastly, method on mytableviewcontroller gets called when notification posted:
func resetplaybutton() { print("resetplaybuttoncalled") // todo: how hold of button , change title outside playmp3? }
you have lot of code, not related problem mentioned above. if understood problem don't know how update button in observer selector resetplaybutton(). button in tableview cell , have stored index in indexpathofplaybutton. can cell like:
let cell: yourcellclass = tableview.cellforindexpath(nsindexpath(foritem: indexpathofplaybutton, insection: 0)) and after cell, can update output.
another way call reloaddata()
Comments
Post a Comment