iosswiftmpmediaitemmpmediaquerympmediaitemcollection

MPMediaQuery.artistsQuery() to show in groups


I know I'm very likely missing something extremely basic here but I'm stuck. Why does the following code produce a table view of each track's artist instead of the artists listed in groups (i.e. why is each artist listed 20-something times instead of being all grouped together so that each artist is only listed once)? I've tried both the collections and items versions mentioned below. What am I doing wrong? How do I fix this? Any help is appreciated. Thanks in advance...

var tableData = MPMediaQuery.artistsQuery()
override func viewDidLoad() {
    super.viewDidLoad()

self.artistTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        }

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.tableData.collections!.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
    let cell: UITableViewCell = self.artistTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

    let artist: MPMediaItem = tableData.items![indexPath.row]
    //let artist: MPMediaItemCollection = tableData.collections![indexPath.row]

    if artist.valueForProperty(MPMediaItemPropertyArtist) == nil {
        cell.textLabel?.text = "Artist" as String
    } else {        
    let artistName = artist.valueForProperty(MPMediaItemPropertyArtist) as! NSString
    cell.textLabel?.text = artistName as String
    }
    return cell
}

Solution

  • I'm going to answer my own question...

    The problem was here:

    let artist: MPMediaItemCollection = tableData.collections![indexPath.row]
    if artist.valueForProperty(MPMediaItemPropertyArtist) == nil {
    

    and is answered here: How to pull the artist value from MPMediaItemCollection

    Thanks to Wes...