致命错误:索引超出范围

编程入门 行业动态 更新时间:2024-10-26 12:23:46
本文介绍了致命错误:索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想展示我的音乐库中的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.

更多推荐

致命错误:索引超出范围

本文发布于:2023-10-18 10:35:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1503981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:索引   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!