使用未解析的标识符“ indexPath”

时间:2018-09-09 01:53:31

标签: ios arrays swift uitableview

尽管我将其更改为小写或大写字母“ 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 :(得分:0)

使用UITableView的委托方法代替您定义的方法-

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

// Here use your indexPath.row

}