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
I tried to change the numberOfRowsInSection
to allSongsArray.count
, but it ends up with the same error.
Generally, list index out of range means means that you are providing an index for which a list element does not exist.
You should return allSongsArray.count
and to avoid being returned empty cells use yourTableView.reloadData
after you fill your allSongsArray
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With