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)")
}
}
}
}
你可以顯示崩潰報告 –
請發佈您的完整logcat輸出,你問爲什麼它崩潰,而不提供日誌輸出,我們不是嚮導。 – Nanoc
它看起來像'titleatcell'數組越界了,你可以刪除'titleatcell',因爲你沒有在你的查詢中使用它。 – deoKasuhal