Ios 为什么在单击阵列中的按钮时应用程序会崩溃?
我有一个带有歌曲标题的数组,我正在给每个单元格添加一个带有标题的播放按钮,但是当我单击播放按钮时,它崩溃了,我将数组从索引错误中取出。为什么会发生这种错误?我怎么修理它Ios 为什么在单击阵列中的按钮时应用程序会崩溃?,ios,swift,Ios,Swift,我有一个带有歌曲标题的数组,我正在给每个单元格添加一个带有标题的播放按钮,但是当我单击播放按钮时,它崩溃了,我将数组从索引错误中取出。为什么会发生这种错误?我怎么修理它 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = table.dequeueReusableCellWithIdentifier
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)")
}
}
}
}
致命错误:数组索引超出范围
尝试访问不存在的数组索引时会发生此错误。可能您没有解析数据,当您单击“开始”按钮时,xcode试图访问一个不存在的指针。。。。但是我们需要更多的信息来帮助您有效地您能显示崩溃报告吗请发布完整的logcat输出,您会问为什么它会崩溃,而不提供日志输出,我们不是向导。看起来titleatcell阵列已经越界了,你可以删除titleatcell,因为你没有在你的查询中使用。我现在无法截屏,但崩溃控制台说的所有错误都是致命错误:数组索引超出范围这是在哪行代码上崩溃的?