iosswiftmedia-playermpmedialibrary

Why is my table view only showing about 20 of my items from an array?


My app is not correctly displaying all of the items in my array. What is causing this to happen?

    var songamount = ["Refresh Page"]
    var songImageAmount = [MPMediaItemArtwork]()

    override func viewDidLoad() {
        super.viewDidLoad()
        MPMediaLibrary.requestAuthorization { (status) in
            let myPlaylistQuery = MPMediaQuery.playlists()
            let playlists = myPlaylistQuery.collections
            self.songamount.remove(at: 0)
            for playlist in playlists! {
                print(playlist.value(forProperty: MPMediaPlaylistPropertyName)!)

                let songs = playlist.items
                for song in songs {
                    let songTitle = song.value(forProperty: MPMediaItemPropertyTitle)
                    let songImage = song.artwork
                    self.songamount.append(songTitle as! String)
                    self.songImageAmount.append(songImage!)
                    print("\t\t", songTitle!)

            }
            print(self.songamount)
            print("Song Amount:\(self.songamount.count)")
            print("Image Amount: \(self.songImageAmount.count)")


            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songamount.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LibraryCell", for: indexPath) as! LibraryCell
        cell.LibraryTitle.text = songamount[indexPath.row]
        //cell.LibraryImage.image = songImageAmount[indexPath.row]
        print(indexPath)

        return cell

    }
}

This is my code to show all the songs in a users Itunes library but it is only displaying 20 items from the array in the tableView.

Update- I have got it to correctly make a list of all my songs but it is only showing 33 of them in the list. Here is the updated code

    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var TextDebug: UILabel!

    var songamount = ["Please Reload View"]
    var songImageAmount = [MPMediaItemArtwork]()

    override func viewDidLoad() {
        super.viewDidLoad()
        MPMediaLibrary.requestAuthorization { (status) in
            let mySongQuery = MPMediaQuery.songs()
            let songs = mySongQuery.collections
            self.songamount.remove(at: 0)
            for song in songs! {
                print(MPMediaItemPropertyTitle)

                let songs = song.items
                for song in songs {
                    let songTitle = song.value(forProperty: MPMediaItemPropertyTitle)
                    //let songImage = song.artwork
                    self.songamount.append(songTitle as! String)
                    //self.songImageAmount.append(songImage!)
                    print("\t\t", songTitle!)

            }
            print(self.songamount)
            print("Song Amount:\(self.songamount.count)")
            //print("Image Amount: \(self.songImageAmount.count)")


            }
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songamount.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "LibraryCell", for: indexPath) as! LibraryCell
        cell.LibraryTitle.text = songamount[indexPath.row]
        //cell.LibraryImage.image = songImageAmount[indexPath.row]
        let sections: Int = tableView.numberOfSections
        var rows: Int = 0

        for i in 0..<sections {
            rows += tableView.numberOfRows(inSection: i)
            TextDebug.text = "\(rows)"
        }


        return cell

    }

Solution

  • The solution to my problem was to add tableView.reloadData() to the end of viewDidLoad()