ios - 为什么我的 TableView 只显示数组中的大约 20 个项目?

标签 ios swift media-player mpmedialibrary

我的应用程序没有正确显示数组中的所有项目。发生这种情况的原因是什么?

    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

    }
}

这是我的代码,用于显示用户 Itunes 库中的所有歌曲,但它仅在 tableView 中显示数组中的 20 个项目。

更新 - 我已经让它正确地列出了我所有的歌曲,但它只在列表中显示了其中的 33 首。这是更新后的代码

    @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

    }

最佳答案

我的问题的解决方案是将 tableView.reloadData() 添加到 viewDidLoad() 的末尾

关于ios - 为什么我的 TableView 只显示数组中的大约 20 个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55779281/

相关文章:

协议(protocol)中的 Swift 元组声明

ios - 如何在 Mapview 中使用动画自定义 Pin(gif 图像)?

ios - 如何检查 HealthKit 功能何时完成(Swift)

ios - ios中通过http请求发送api键值(生产键)

string - 绝对需要复制字符串的情况

android - 搜索栏中显示的音乐缓冲级别

java - 按下后退按钮并再次打开应用程序后媒体播放器崩溃

java - MediaPlayer android 流广播不立即启动或根本不启动

ios - 阅读IOS6中的请勿打扰设置

objective-c - 如何在NSString中使用文本和变量的组合?