Как добавить данные в первую строку таблицы, прежде чем она будет заполнена элементами MPmedia из запроса? - PullRequest
0 голосов
/ 04 февраля 2019

Я медленно собирал музыкальный плеер.Я запрашиваю данные альбома, чтобы заполнить таблицу.Если пользователь выбирает альбом, мы переходим на новый экран, чтобы выбрать песню для воспроизведения.Я хотел бы сделать первую строку таблицы альбомов (в контроллере представления альбомов) строкой под названием «Все песни», но я не смог понять, как это сделать.

Я пытался использовать: 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
    }

1 Ответ

0 голосов
/ 04 февраля 2019

Просто убедитесь, что вы отображаете на одну строку больше, чем у вас есть альбомы запросов, а затем каждый раз, когда вам нужно получить альбом запросов, вы минус 1 из indexPath.row

    @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]
        }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...