我怎样才能之前它充满了从查询MPmedia项数据添加到表格的第一行?

问题描述 投票:0回答:1

我一直在慢慢地建立一个音乐播放器。我查询专辑数据来填充表。如果用户选择一个专辑,我们Segue公司到一个新的屏幕挑选要播放的歌曲。我想使专辑表格的第一行(在相册查看控制器),是一个名为“所有歌曲”行,但我一直无法弄清楚如何做到这一点。

我曾尝试使用:insertRowsAtIndexPaths。但没有成功。

    @IBOutlet var albumsTableView: UITableView!

    let qryAlbums = MPMediaQuery.albums()  // Query the albums

    // Set the cell in the table
    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

        // I am using the custom class that I created called: myCustomAlbumTableViewCell
        let cell = tableView.dequeueReusableCell(withIdentifier: "albumIdCell", for: indexPath) as! myCustomAlbumTableViewCell

        let rowItem = qryAlbums.collections![indexPath.row]

        cell.albumTitle.text = rowItem.items[0].albumTitle

        return cell
    }
swift collections tableview xcode9 mpmediaquery
1个回答
1
投票

只要确保你显示一个多行比你查询的专辑,然后每次需要从indexPath.row得到查询专辑你减去1

    @IBOutlet var albumsTableView: UITableView!

    let qryAlbums = MPMediaQuery.albums()  // Query the albums

    // Set the cell in the table
    func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell {

        // I am using the custom class that I created called: myCustomAlbumTableViewCell
        let cell = tableView.dequeueReusableCell(withIdentifier: "albumIdCell", for: indexPath) as! myCustomAlbumTableViewCell

        if indexPath.row == 0 {
            cell.albumTitle.text = "All Songs"
        } else {

            let rowItem = qryAlbums.collections![indexPath.row-1]

            cell.albumTitle.text = rowItem.items[0].albumTitle
        }

        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) ->  Int {
        return qryAlbums.collections! .count + 1
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row > 0 {
            let rowItem = qryAlbums.collections![indexPath.row-1]
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.