Использование неразрешенного идентификатора indexPath - PullRequest
0 голосов
/ 09 сентября 2018

Хотя я изменил его в нижнем или верхнем регистре 'i', это все равно ошибка.

Вот мой код:

import UIKit
import AVFoundation

var songs:[String] = []
var audioPlayer = AVAudioPlayer()

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var tblview: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        gettingSongName()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return songs.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell"); cell.textLabel?.text = songs[indexPath.row]
        return cell
    }

    func gettingSongName(){
        let folderURL = URL(fileURLWithPath: Bundle.main.resourcePath!)//declare a variable to set the URL path for the resource file
        do{
            let songPath = try FileManager.default.contentsOfDirectory(at: folderURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
            for song in songPath{
                var mysong = song.absoluteString
                if mysong.contains(".mp3")
                {
                    let findstring = mysong.components(separatedBy: "/")
                    mysong = findstring[findstring.count-1]
                    mysong = mysong.replacingOccurrences(of: "%20", with: " "); mysong = mysong.replacingOccurrences(of: ".mp3", with: "")
                    songs.append(mysong)
                }
            }
            tblview.reloadData()
        }
        catch{ }
    }

    func didSelectRowatIndexPath(){
        do{
            let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3")
            try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
            audioPlayer.play()
        }
        catch
        {}
    }
}

Ошибка появляется на func didSelectRowatIndexPath() в этой строке кода:

let audioPath = Bundle.main.path(forResource: songs[indexPath.row], ofType: ".mp3")

Что мне делать? Я использую Xcode 9.4.

1 Ответ

0 голосов
/ 09 сентября 2018

Используйте метод делегата UITableView вместо вашего определенного метода -

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{

// Here use your indexPath.row

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...