致命错误:索引超出范围 [英] fatal error: Index out of range

查看:73
本文介绍了致命错误:索引超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于致命错误:索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆