Search code examples
iosswiftuitableviewavaudioplayer

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


Here is my code. When I play the song from the search bar, it does not correspond to the actual song I intended to play. Any suggestions would be appreciated.

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

Solution

  • You do the same thing you do when you are not searching except you get the path from filteredArray instead of 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")
        }
    }
    

    And you really need to move these lines:

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

    to the inside of your class. These should be class properties, not globals.