发布NSNotification时更新自定义单元格UIButton标题

时间:2015-10-22 15:08:48

标签: ios swift uitableview uibutton swift2

我有一个UITableViewController,其中有一堆自定义单元格,其中包含playButton。在cellForRowAtIndexPath中,我指定的标签等于按钮的indexPath,这样当按下playButton时,我将按钮的标题设置为“停止”。它应该变为“停止”,当我按下“停止”时,它会变回“播放”。

我遇到困难的地方是我的声音停止播放,没有用户干预。我建立了一个观察员来听MP3播放器的完成。我在viewDidLoad的{​​{1}}中添加了一位观察员:

以下是我用来帮助更改单元格中MyTableViewController标题的变量:

playButton

// Variables to facilitate changing playButton title var indexPathOfPlayButton = Int() var isPlaying: Bool = false 的{​​{1}}中,我添加了这个观察者:

viewDidLoad

以下是MyTableViewController上的 NSNotificationCenter.defaultCenter().addObserver(self, selector: "resetPlayButton", name: resetPlayButtonNotification, object: nil) 方法:

playMP3

在我的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()
    }
}

最后,这是MP3Player上发布通知时调用的方法:

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)
    }
}

2 个答案:

答案 0 :(得分:0)

您有很多代码,与您上面提到的问题无关。如果我了解您的问题是您不知道如何更新观察者选择器resetPlayButton()中的按钮。该按钮位于tableview单元格中,您已将索引存储在indexPathOfPlayButton中。 您可以获得如下单元格:

let cell: YourCellClass = tableView.cellForIndexPath(NSIndexPath(forItem: indexPathOfPlayButton, inSection: 0))

获得单元格后,您可以更新其输出。

另一种方法是致电reloadData()

答案 1 :(得分:0)

神秘解决了!我将发件人的indexPath设置为Int。相反,我声明了这个类变量:

// I removed this--> var indexPathOfPlayButton = Int()
var clickedButton = UIButton() // this gets set in playMP3

playMP3中,我添加了语句并取出了我设置indexPath变量的语句:

    let indexPath = sender.tag // The tag which button was pressed (it's set to its indexPath when it's drawn in cellForRowAtIndexPath)
    // Nuked this--> indexPathOfPlayButton = indexPath
    // and replaced with this
    clickedButton = sender as! UIButton

然后,在我的resetPlayButton方法中,我抓住该变量并将标题更改回" Play"

func resetPlayButton() {
    print("resetPlayButtonCalled")
    clickedButton.setTitle("Play", forState: UIControlState.Normal)
}