2015-11-05 133 views
0

我有一個歌曲標題數組,我添加一個播放按鈕到每個單元格的標題,但是當我點擊播放按鈕它崩潰,我得到數組超出索引錯誤。爲什麼會發生此錯誤?我如何解決它?爲什麼點擊數組中的按鈕時應用程序崩潰?

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

    let cell = table.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) 

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

    let playButton : UIButton = UIButton(type: UIButtonType.Custom) 
    playButton.tag = indexPath.row 
    let imageret = "playbutton" 
    playButton.setImage(UIImage(named: imageret), forState: .Normal) 
    playButton.frame = CGRectMake(230, 20, 100, 100) 
    playButton.addTarget(self,action: "playit:", forControlEvents: UIControlEvents.TouchUpInside) 

    for view: UIView in cell.contentView.subviews { 
     view.removeFromSuperview() 
    } 


    cell.contentView.addSubview(playButton) 

    return cell 
} 

下面是根據點擊播放按鈕播放歌曲的代碼。

func playit(sender: UIButton!){ 

    let cell = table.dequeueReusableCellWithIdentifier("cell") 

    let playButtonrow = sender.tag 

    print(titleatcell[playButtonrow]) 



    if let nowPlaying = musicPlayer.nowPlayingItem{ 
    let title = nowPlaying[MPMediaItemPropertyTitle] as? String 
    let artist = nowPlaying[MPMediaItemPropertyTitle] as? String 



    print("now playing \(title!) \(artist!)") 
    print("cell: \(playButtonrow) \(titleatcell[playButtonrow])") 

     let query = PFQuery(className: "Songs") 
     query.whereKey("SongName", equalTo: ret[playButtonrow]) 
     query.findObjectsInBackgroundWithBlock { 
      (objects: [AnyObject]?, error: NSError?) -> Void in 
      if error == nil { 
       // The find succeeded. 
       print("Successfully retrieved \(objects!.count) song(s).") 
       // Do something with the found objects 
       if let objects = objects as? [PFObject] { 
        for object in objects { 
         print(object.objectId) 

         print(playButtonrow) 
         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) 
         player.volume = 1.0 
         //let timeInterval: NSTimeInterval = 10.0 
         //let timesArray = [NSValue(CMTime: CMTimeMake(Int64(timeInterval), 1))] 
         timeObserver = player.addBoundaryTimeObserverForTimes([30.0], queue:nil) {() -> Void in 
          print("30s reached", terminator: "") 
          player.removeTimeObserver(timeObserver) 
          player.pause() 
         } 
         player.play() 





        } 


       } 

      } else { 
       // Log details of the failure 
       print("Error: \(error!) \(error!.userInfo)") 
      } 


     } 

    } 
} 
+0

你可以顯示崩潰報告 –

+0

請發佈您的完整logcat輸出,你問爲什麼它崩潰,而不提供日誌輸出,我們不是嚮導。 – Nanoc

+1

它看起來像'titleatcell'數組越界了,你可以刪除'titleatcell',因爲你沒有在你的查詢中使用它。 – deoKasuhal

回答

0

「致命錯誤:數組索引超出範圍」

當您試圖訪問一個不存在的數組索引出現此錯誤。也許你沒有得到解析數據,當你點擊按鈕開始,Xcode試圖訪問一個不存在的指針....但我們需要更多的信息來有效地幫助你

相關問題