为什么点击数组中的按钮时应用程序崩溃?

问题描述:

我有一个歌曲标题数组,我添加一个播放按钮到每个单元格的标题,但是当我点击播放按钮它崩溃,我得到数组超出索引错误。为什么会发生此错误?我如何解决它?为什么点击数组中的按钮时应用程序崩溃?

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

“致命错误:数组索引超出范围”

当您试图访问一个不存在的数组索引出现此错误。也许你没有得到解析数据,当你点击按钮开始,Xcode试图访问一个不存在的指针....但我们需要更多的信息来有效地帮助你