使用未解析的标识符'indexPath'

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

虽然我用小写或大写'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。

ios arrays swift uitableview
1个回答
0
投票

使用UITableView的委托方法而不是您定义的方法 -

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

// Here use your indexPath.row

}
© www.soinside.com 2019 - 2024. All rights reserved.