【问题标题】:How can I add data to the first row of a table before it is filled with MPmedia items from a query?如何在表的第一行填充来自查询的 MPmedia 项目之前将数据添加到表的第一行?
【发布时间】:2019-06-27 17:52:21
【问题描述】:

我一直在慢慢构建一个音乐播放器。我查询专辑数据以填充表格。如果用户选择一张专辑,我们会转到一个新屏幕来选择要播放的歌曲。我想制作专辑表的第一行(在专辑视图控制器中),成为名为“所有歌曲”的行,但我无法弄清楚如何做到这一点。

我曾尝试使用: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】:

    只需确保显示的行数比查询专辑多一列,然后每次需要获取查询专辑时,从 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]
            }
        }
    

    【讨论】:

    • 感谢您的帮助!它正在部分工作。用户必须能够选择第一行(“所有歌曲”)。然后当我转到下一个视图控制器时,我知道要列出该艺术家所有专辑中的所有歌曲。例如,如果艺术家在设备的音乐库中有 4 张专辑,我会列出“所有歌曲”,然后是 4 张专辑的标题。在测试中,前 4 行有效,但第五行崩溃。
    • 我一直在使用 didSelectRowAtIndexPath... func tableView(_ tableView: UITableView, didSelectRowAtIndexPath indexPath: IndexPath) { self.performSegue(withIdentifier: "rowSelectedGotoSongsVC", sender: tableView) }
    • 我不确定我是否理解问题所在。您的 tableView 中有 5 行。第一个是“所有歌曲”。其他 4 个是专辑名称。据我了解,这是正常工作和显示的。但是,如果您点击最后一行,应用程序会崩溃吗?听起来您在获取实际行项目时没有从 indexPath.row 中减去 1:let rowItem = qryAlbums.collections![indexPath.row-1]
    • 你是对的!我已经从你说的地方(在 cellForRowAtIndexPath 内)中减去了 1,但是在处理 segue 的代码中忘记了这样做(我昨天忘记发布该部分)。无论如何,虽然我需要进行更多测试,但您的解决方案有效。非常感谢您帮助我!!!
    猜你喜欢
    • 2020-04-23
    • 2013-08-23
    • 2011-01-26
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 2013-12-29
    相关资源
    最近更新 更多