Home>
Current status

If i set up a searchBar in TableView and enter characters, you will receive search results from the API accordingly.
Although it is displayed in TableView, the Searchbar disappears from the top when scrolling and scrolls for a few minutes of Cell.
It will come out at the bottom where it is finished.

I think there are things like using Header and defining it in SearchBarController (?).
What is the best thing in the current environment?

At the top before typing and pressing enter

When you press enter and the cell gets information, it stays at the bottom scrolled down.
It does not change even if you search for the character again.

import UIKit
import PKHUD
class MovieSearchViewController: UIViewController, UISearchBarDelegate {
    @IBOutlet weak var table: UITableView!
    @IBOutlet weak var search: UISearchBar!
    @IBOutlet weak var indicater: UIActivityIndicatorView!
    private var items: [MovieItem] = [MovieItem] ()
    var alertController: UIAlertController!
    override func viewDidLoad () {
        super.viewDidLoad ()
        self.setupTable ()
        self.setupNavigationBackground ()
        }
    }
    // Error alert
    func alert (title: String, message: String) {
        alertController = UIAlertController (title: title,
                                   message: message,
                                   preferredStyle: .alert)
        alertController.addAction (UIAlertAction (title: "OK", "
                                       style: .default,
                                       handler: nil))
        present (alertController, animated: true)
    }
    // SearchButtounClicked
    func searchBarSearchButtonClicked (_ searchBar: UISearchBar) {
        view.endEditing (true)
        fechData ()
    }
    func fechData () {
        search.resignFirstResponder ()guard let text = search.text,! text.isEmpty else {
            return
        }
        guard let query = text.addingPercentEncoding (withAllowedCharacters: .urlQueryAllowed) else {
            print ("percent-encoding failure")
            return
        }
        // LoadingAction Start
        HUD.show (.progress)
        items.removeAll ()
        URLSession.shared.dataTask (with: URL (string: "https://api.themoviedb.org/3/search/movie?api_key=MYAPIKEY&language=ja&query=\(query)&page=1") !,
                                   completionHandler: {data, response, error in
                                    guard let data = data else {
                                        print (error? .localizedDescription ?? "Unknown error")
                                        return
                                    }
                                    var result: MovieStruct?
                                    do {
                                        result = try JSONDecoder (). decode (MovieStruct.self, from: data)
                                    }
                                    catch {
                                        print ("JSON perse error")
                                    }
                                    guard let finalResult = result else {
                                        return
                                    }
                                    let newMovies = finalResult.results
                                    self.items.append (contentsOf: newMovies)
                                    DispatchQueue.main.async {
                                        self.table.reloadData ()
                                        // Search error message
                                        if newMovies.isEmpty {
                                            self.alert (title: "Error", message: "No matching movie")
                                        }
                                        // LoadingAction Stop
                                        HUD.flash (.progress)
                                    }
            }) .resume ()
    }
}
extension MovieSearchViewController {
    private func setupTable () {table.delegate = self
        table.dataSource = self
        search.delegate = self
        table.register (TableViewCell.nib (), forCellReuseIdentifier: TableViewCell.identifier)
    }
    func searchBarShouldEndEditing (_ searchBar: UISearchBar)->Bool {
        search.text = ""
        search.autocapitalizationType = .none
        return true
    }
}
extension MovieSearchViewController: UITableViewDataSource {
    func tableView (_ tableView: UITableView, numberOfRowsInSection section: Int)->Int {
        return items.count
    }
    func tableView (_ tableView: UITableView, cellForRowAt indexPath: IndexPath)->UITableViewCell {
        let cell = table.dequeueReusableCell (withIdentifier: TableViewCell.identifier, for: indexPath) as! TableViewCell
        cell.setcell (item: items [indexPath.row])
        return cell
    }
}
extension MovieSearchViewController: UITableViewDelegate {
    func tableView (_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow (at: indexPath, animated: true)
        self.hidesBottomBarWhenPushed = true
        performSegue (withIdentifier: "registerControl", sender: items [indexPath.row])
        self.hidesBottomBarWhenPushed = false
    }
    override func prepare (for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "registerControl" {
            let nextVC: RegisterViewController = segue.destination as! RegisterViewController
            if let selectedMovies = sender as? MovieItem {
                nextVC.movie = selectedMovies
            }
        }
    }
}
Digression

When I enter letters in the Search Bar, the first letter starts with a capital letter.
I want to lowercase the first letter of the Search Bar

I changed it with reference to this, but it is not reflected.
It's a digression because it doesn't matter if you use capital letters, but if anyone can understand it, please let me know.