如何指定播放按钮仅播放解析中的第一个对象?

时间:2015-07-21 08:21:04

标签: swift parse-platform

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

1 个答案:

答案 0 :(得分:0)

因此,如果您UITableViewCell中的每首歌曲目前为UITableView,则应尝试使用UIButton嵌入每个单元格。首先在单独的文件中创建自定义UITableViewCell。在我的示例中,我将UIButton拖动到Storyboard中的原型单元格中,根据需要进行定位。然后在新文件中创建自定义UITableViewCell,比如说MyCustomCell.swift。不要忘记将Storyboard中的原型单元与自定义单元类连接起来。然后在故事板中拖动按钮:

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

然后您可以在cellForRowAtIndexPath函数中执行此操作:

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