本文介绍了致命错误:索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想展示我的音乐库中的25首歌曲.这是我的代码:
I want to show 25 of the songs I have in my library. This is my code:
var allSongsArray: [MPMediaItem] = [] let songsQuery = MPMediaQuery.songsQuery() func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 25 //allSongsArray.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell") let items = allSongsArray[indexPath.row] cell?.textLabel?.text = items.title cell?.detailTextLabel?.text = items.artist cell?.imageView?.image = items.artwork?.imageWithSize(imageSize) return cell! }当我运行它时,它崩溃了,因为:
When I run this, it crashes, because:
致命错误:索引超出范围
fatal error: Index out of range
我试图将 numberOfRowsInSection 更改为 allSongsArray.count ,但最终出现相同的错误.
I tried to change the numberOfRowsInSection to allSongsArray.count, but it ends up with the same error.
推荐答案您应返回 allSongsArray.count ,为避免返回空单元格,请在您使用后使用 yourTableView.reloadData 填写您的 allSongsArray .
You should return allSongsArray.count and to avoid being returned empty cells use yourTableView.reloadData after you fill your allSongsArray.
更多推荐
致命错误:索引超出范围
发布评论