2015-10-22 100 views
0

我有一個UITableViewController與一堆自定義單元格中有一個playButton。在cellForRowAtIndexPath中,我分配了一個標籤,該標籤等於按鈕的indexPath,以便按下playButton時,我將按鈕的標題設置爲「停止」。它會更改爲「停止」,因爲它應該,而當我按下「停止」時,它會變回「播放」。當NSNotification發佈時更新自定義單元格UIButton標題

我遇到困難的時候我的聲音停止播放,沒有用戶干預。我建立了一個觀察者來聽MP3播放器的完成。我加入viewDidLoadMyTableViewController的觀察員:

這裏是變量我使用的方便改變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) 

這裏是我的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) 
    } 
} 

最後,這是當一個通知發佈時調用上MyTableViewController方法:

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

回答

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方法,我搶變量並將標題改回「播放」

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

您有很多代碼,這與您上面提到的問題無關。如果我明白你的問題是你不知道如何更新觀察者選擇器resetPlayButton()中的按鈕。該按鈕位於tableview單元格中,並且您已將索引存儲在indexPathOfPlayButton中。 你可以得到的細胞,如:

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

,你會得到的細胞後,就可以更新其輸出。

另一種方法是調用reloadData()

+0

謝謝您的迴應。我想到了。我沒有在'playMP3'中設置indexPath變量,而是在該類中添加了一個按鈕變量,並將其設置爲'playMP3'。 – Adrian

相關問題