我有一个带有歌曲标题的数组,我正在为每个单元格添加一个带有标题的播放按钮,但是当我单击播放按钮时它崩溃了,我得到的数组没有索引错误。为什么会出现此错误?我该如何解决?
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)
“致命错误:数组索引超出范围”
当您尝试访问不存在的数组索引时,会发生此错误。也许你没有解析数据,当你点击按钮开始时,xcode试图访问一个不存在的指针......但是我们需要更多的信息来帮助你有效地