I am using Xcode 10.3 and have written a music program that has evolved over a long time and almost completely works. When I use query = MPMediaQuery.songs(), I can use the index on the righthand side of the screen perfectly. But, when I use query = MPMediaQuery.albums(), I can only get the album titles that that start with A. That's it. Yet there are 464 albums (only 27 start with the letter A).
This may appear to be a "repeat question". Most of this code was written by weyney nearly 4 years ago. It worked for him then. Swift has changed since then. When I use numberOfSectionsInTableView (with albums, like he did) I get the first 27 songs under "A". When I use numberOfSections (with albums) it will crash (but not for songs).
You'll notice that I never get any printed output for index or for the number of sections.
let qryAlbums = MPMediaQuery.albums()
query.groupingType = MPMediaGrouping.album
// Display the index of the table on the right-hand side of the screen
func sectionIndexTitles(for tableView: UITableView) -> [String]? {
let sectionIndexTitles = query.itemSections!.map { $0.title }
print("sectionIndexTitles = ", sectionIndexTitles)
return sectionIndexTitles
}
// Set the alignment of the section header in the table
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
header.textLabel!.textAlignment = NSTextAlignment.center
}
// Get the index of the section
func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
print("index = ", index)
return index
}
// Display a header title in each section
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
print("query.itemSections![section].title = ", query.itemSections![section].title)
return (query.itemSections![section].title)
}
// Get the number of sections
func numberOfSectionsInTableView(in tableView: UITableView) -> Int {
print("query.itemSections?.count = ", query.itemSections?.count ?? 0)
return (query.itemSections?.count)!
}
// Get the number of rows per Section - YES SECTIONS EXIST WITHIN QUERIES
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("query![section].range.length = ", query.collectionSections![section].range.length)
return query.collectionSections![section].range.length
}
// Set the cell in the table
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// I am using the custom class that I created called: myCell
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! myTableViewCell
let currLoc = query.collectionSections![indexPath.section].range.location
let rowItem = query.collections![indexPath.row + currLoc]
// Main text is Album name
cell.title.text = rowItem.items[0].albumTitle
}
Output:
query.itemSections![section].title = A
query![section].range.length = 27
query.itemSections![section].title = A
query![section].range.length = 27
query.itemSections![section].title = A
query![section].range.length = 27
sectionIndexTitles = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#", "?"]
query.itemSections![section].title = A
I pretty much spent most of the day trying to find the solution. I would really appreciate any help. Thanks ahead of time!
I finally found it. I had to replace a ? with a ! (and cut down the count) in the func numberOfSections.
Hope this helps someone else who may be struggling with it.