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

agxfikkp  于 2022-11-06  发布在  Swift
关注(0)|答案(3)|浏览(157)

我有一个UITableViewController,其中有一组自定义单元格,其中有一个playButton。在cellForRowAtIndexPath中,我分配了一个等于按钮的indexPath的标记,这样当按下playButton时,我将按钮的标题设置为“Stop”。它会更改为“Stop”,这是应该的,当我按下“Stop”时,它会更改回“Play”。
我遇到的困难是当我的声音在没有用户干预的情况下自动停止播放时。我设置了一个观察器来监听MP3播放器正在播放的声音。我在MyTableViewControllerviewDidLoad中添加了一个观察器:
下面是我用来更改单元格中playButton的标题的变量:

// Variables to facilitate changing playButton title
var indexPathOfPlayButton = Int()
var isPlaying: Bool = false

MyTableViewControllerviewDidLoad中,我添加了这个观察者:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "resetPlayButton", name: resetPlayButtonNotification, object: nil)

下面是MyTableViewController上的playMP3方法:

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

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

func resetPlayButton() {
    print("resetPlayButtonCalled")
    // TODO: How do I get a hold of the button and change the title from outside playMP3?
}
edqdpe6u

edqdpe6u1#

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

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

在获得单元格后,可以更新其输出。
另一种方法是调用reloadData()

ukqbszuj

ukqbszuj2#

谜底解开了!我将发送方的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)
}
vfhzx4xs

vfhzx4xs3#

对我来说,它是使用主线程发布通知:

DispatchQueue.main.async {
     NotificationCenter.default.post(name: Notification.Name("identifier"), object: true, userInfo: ["text": "Purchased"])
}

相关问题