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

时间:2018-05-06 06:20:32

标签: ios swift uitableview avaudioplayer

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

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()
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

除非您从myArray.filter(function(el){return el.name == "John"})[0].age 而不是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")
    }
}

到你班级的内部。这些应该是类属性,而不是全局变量。