【问题标题】:Why is my table view only showing about 20 of my items from an array?为什么我的表格视图只显示数组中的大约 20 个项目?
【发布时间】:2019-09-10 18:05:41
【问题描述】:

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

    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

    }

【问题讨论】:

  • songamount.count 返回什么?
  • @Koen 它返回 472,由于某种原因,这实际上超过了我库中的歌曲数量。我现在正在研究这个
  • 确保在完成数据模型更新后重新加载表视图(在主队列上)。
  • 并且你需要在requestAuthorization完成块中检查status的值。仅当状态为 .authorized 时,您才应尝试访问歌曲。
  • @rmaddy 我已经在应用程序的早期部分运行了 .authorized 代码,除非授权访问,否则无法通过该代码

标签: ios swift media-player mpmedialibrary


【解决方案1】:

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

【讨论】:

  • 不,解决方案是在完成块内的for 循环结束后调用reloadData()
  • @rmaddy 我所做的工作,所以这对我来说是解决方案,它也可以工作,但不是我所做的。
  • 在异步完成处理程序之后调用 reloadData 在大多数情况下可能无法正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-14
  • 2016-02-05
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多