2015-07-21 32 views
0

我在Parse上有兩首歌曲,我可以讓它們用swift播放,但是我想爲每個對象播放/暫停按鈕。歌曲1應該有一個播放/暫停按鈕,歌曲2應該有一個播放/暫停按鈕,等等。我如何讓每個按鈕在表格視圖中用於x對象?我已經嘗試將參數添加到AudioPlayer.play()AudioPlayer.play(objectId[0]),但它不起作用。如何分配播放按鈕僅播放解析中的第一個對象?

func playit (sender: UIButton!){ 
    switch(state){ 
    case 0: 
     AudioPlayer.play() 
     println("Playing"); 
     let pauseimage = "pause" 
     play.setImage(UIImage(named: pauseimage), forState: .Normal) 
     state = 1 
     break; 
    case 1: 
     AudioPlayer.pause() 
     println("Paused"); 
     let playimage = "player" 
     play.setImage(UIImage(named:playimage), forState: .Normal) 
     state = 0 
     break; 

    default: break; 
    } 

} 


     var ObjectIDQuery = PFQuery(className: "Songs") 
     //find objects in background 
     ObjectIDQuery.findObjectsInBackgroundWithBlock({ 
      //store objects in an array 
      (objectsArray: [AnyObject]?, error: NSError?) -> Void in 

      var objectIDs = objectsArray as! [PFObject] 
      NSLog("\(objectIDs)") 
      //objects being added to idArray 
      for i in 0...objectIDs.count-1{ 
       //add a new element in the array 
       self.iDArray.append(objectIDs[i].valueForKey("objectId")as! String) 
       //store song name in song array 
       self.NameArray.append(objectIDs[i].valueForKey("Name")as! String) 
       self.tableView.reloadData() 

      } 
     }) 


    } 

    //select a certain song from tableview 
    func grabSong(){ 
     var SongQuery = PFQuery(className: "Songs") 
     //taking x object from idArray depending on which cell is selected 
     SongQuery.getObjectInBackgroundWithId(iDArray[SelectedSongNumber], block: { 
      //store object inside pfobject 
      (object: PFObject?, error: NSError?) -> Void in 
      //take objects grabbed and put in audioplayer 
      if let AudioFileURLTemp = object?.objectForKey("File")?.url{ 
       AudioPlayer = AVPlayer(URL: NSURL(string: AudioFileURLTemp!)) 
       AudioPlayer.play() 

      } 
     }) 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return iDArray.count 

    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! UITableViewCell 

     cell.textLabel?.text = NameArray[indexPath.row] 
     return cell 
    } 

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     SelectedSongNumber = indexPath.row 
     grabSong() 
     } 
+0

如果每個

class MyCustomCell: UITableViewCell{ @IBOutlet weak var playButton: UIButton! //Declare other cell attributes here // ...... } 

然後你就可以在你的cellForRowAtIndexPath功能做到這一點歌曲是在'UITableView'中單獨的'UITableViewCell',你有沒有試過在每個單元格中嵌入一個按鈕? –

+0

我將如何實現每個單元的按鈕? – tanman

回答

0

所以,如果您的每首歌曲,如果目前是UITableViewCell裏面你UITableView你應該嘗試嵌入每個單元有UIButton。首先在一個單獨的文件中製作一個自定義的UITableViewCell。在我的例子中,我將UIButton拖入我的原型單元格Storyboard中,按照我的需要進行定位。然後在一個新文件中創建一個自定義UITableViewCell,讓我們說MyCustomCell.swift。不要忘記將您的Storyboard中的原型單元與您的自定義單元類連接起來。然後,從故事板拖動按鈕:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      var cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! MyCustomCell 

      cell.textLabel?.text = NameArray[indexPath.row] 

      cell.playButton.layer.setValue(indexPath.row, forKey: "index") 
      cell.playButton.addTarget(self, action: "playSong:", forControlEvents: UIControlEvents.TouchUpInside) 
      return cell 
     } 

而且你UITableViewController文件中創建函數:

func playSong(sender: UIButton){ 
    let cellIndex : Int = (sender.layer.valueForKey("index")) as! Int 
    //You now have the index of the cell whose play button was pressed so you can do something like 
    //arrayOfSongs[cellIndex].play 
    self.tableView.reloadData() 
    }