如何在播放时实现与歌曲对应的搜索?

How can I implement a search that correspond to the song when it's played?

这是我的代码。当我从搜索栏播放歌曲时,它与我打算播放的实际歌曲不对应。如有任何建议,我们将不胜感激。

import UIKit
import AVFoundation

var thisSong4 = 0
var audioStuffed4 = false
var filteredArray = [String]()

let songsArray4 = ["1", "2", "3", "4", "5", "6"]

class SongsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UITextFieldDelegate {
    @IBOutlet var searchBar: UISearchBar!
    @IBOutlet var myTableView: UITableView!

    var isSearching = false

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell

        cell.textLabel?.text = filteredArray[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if isSearching == false{
            do{
                let audioPath = Bundle.main.path(forResource: songsArray4[indexPath.row], ofType: ".m4a")
                try audioPlayer = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL)
                audioPlayer.play()
                thisSong4 = indexPath.row
                audioStuffed4 = true
            }
            catch{
                print("ERROR")
            }
        }else {
           //what should I fill in here?
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        searchBar.delegate = self
        searchBar.returnKeyType = UIReturnKeyType.done
        myTableView.dataSource = self
        filteredArray = songsArray4
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return true;
    }

    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String){
        if searchBar.text == nil || searchBar.text == "" {
            isSearching = false

            self.view.endEditing(true)

            searchBar.perform(#selector(self.resignFirstResponder), with: nil, afterDelay: 0.1)

            filteredArray = songsArray4

            myTableView.reloadData()
        } else {
            isSearching = true

            filteredArray = searchText.isEmpty ? songsArray4: songsArray4.filter{(item: String) -> Bool in return item.range(of: searchText, options: .caseInsensitive, range: nil, locale: nil) != nil
            }

            if searchText != "" {
                isSearching = true
                myTableView.reloadData()
            }
        }
    }
}

除了从 filteredArray 而不是 songsArray4.

获取路径外,您在不搜索时会执行相同的操作
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let array = isSearching ? filteredArray : songsArray4

    do {
        let audioURL = Bundle.main.url(forResource: array[indexPath.row], withExtension: ".m4a")!
        try audioPlayer = AVAudioPlayer(contentsOf: audioURL)
        audioPlayer.play()
        thisSong4 = indexPath.row
        audioStuffed4 = true
    } catch {
        print("ERROR")
    }
}

你真的需要移动这些行:

var thisSong4 = 0
var audioStuffed4 = false
var filteredArray = [String]()
let songsArray4 = ["1", "2", "3", "4", "5", "6"]

到你的 class 里面。这些应该是 class 属性,而不是全局属性。