0

When I play song it gets recorded in my app. The song title, artist, etc. is listed in a cell. Every time I go to the next song a new cell is added. Currently in each cell the title and artist are listed as well as a button. Whenever I click the button my program will check if that song is in parse and if it is it will play that file. I.e. if song a is chosen it will check if song a is in parse and if it is it will play that file, same with song b, but if I go back and click the play button for the first song it plays the second song. How do I get each button in each cell to play the corresponding song in that cell? I know what the problem is. Whenever I click the play button it retrieves the string of the currently playing song, not the string of x cell.

func playit(sender: UIButton!){
if let nowPlaying = musicPlayer.nowPlayingItem{
let title = nowPlaying[MPMediaItemPropertyTitle] as? String
let artist = nowPlaying[MPMediaItemPropertyTitle] as? String

println(title! + artist!)


    let query = PFQuery(className: "Songs")
    query.whereKey("SongName", equalTo: title!)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) song(s).")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {
                    println(object.objectId)
                    let playButtonrow = sender.tag
                    let object = object as PFObject
                    let parseAudio = object.valueForKey("SongFile") as! PFFile
                    let audioPath: String = parseAudio.url!
                    let urlParse: NSURL = NSURL(string: audioPath)!



                    player = AVPlayer(URL: urlParse)
                    println(player)
                    player.volume = 1.0
                    player.play()
                    if (player.rate > 0) && (player.error == nil) {
                        // player is playing
                        println("Playing")
                    } else {
                        println("Not Playing")
                    }
                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }

    }

}
}
 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: UITableViewCell = self.table.dequeueReusableCellWithIdentifier("cell") as! UITableViewCell
var play: Play


play = add[indexPath.row]
let playButton : UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
playButton.tag = indexPath.row
let imageret = "playbutton"
playButton.setImage(UIImage(named: imageret), forState: .Normal)
playButton.frame = CGRectMake(236, 20, 100, 100)
playButton.addTarget(self,action: "playit:", forControlEvents: UIControlEvents.TouchUpInside)

for view: UIView in cell.contentView.subviews as! Array<UIView> {
    view.removeFromSuperview()
}
cell.contentView.addSubview(playButton)
blee
  • 297
  • 4
  • 13
  • You are using the tag of the play button to store the indexPath.row that corresponds to the cell, so you need to use that to index into your songs array in `playIt` rather than retrieving the currently playing song. – Paulw11 Sep 20 '15 at 23:37
  • You should update your previous question with your new code rather than asking a duplicate question – Paulw11 Sep 20 '15 at 23:39
  • How would I use the playButton to index the songs array? @Paulw11 – blee Sep 20 '15 at 23:51
  • You have stored the index of the row in the `tag` property - simply retrieve that and you know which row the button is for – Paulw11 Sep 20 '15 at 23:59
  • That's currently what's going on, I can currently get the row where the button is clicked, but I'm trying to get the string(s) in the certain cell (the cell where the button is clicked). – blee Sep 21 '15 at 00:02
  • First, don't use your cell as a data model - it simply displays information that should be stored elsewhere. Second, you are retrieving the row inside the query - this is too late, you need to retrieve it at the start of the handler to see what you should be querying. – Paulw11 Sep 21 '15 at 00:08
  • What do you recommend I should to to fix the third problem? – blee Sep 21 '15 at 00:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90186/discussion-between-paulw11-and-blee). – Paulw11 Sep 21 '15 at 00:10

0 Answers0