-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchViewController.swift
59 lines (45 loc) · 1.81 KB
/
SearchViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import UIKit
class SearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var searchBar: UISearchBar!
var searchResults: [Anime] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
searchBar.delegate = self
tableView.register(UINib(nibName: "AnimeTableViewCell", bundle: nil), forCellReuseIdentifier: "AnimeCell")
}
// MARK: - UITableViewDelegate and UITableViewDataSource methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchResults.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "AnimeCell", for: indexPath) as! AnimeTableViewCell
let anime = searchResults[indexPath.row]
cell.configure(with: anime)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// Navigate to the anime details screen and pass the selected anime object
}
// MARK: - UISearchBarDelegate methods
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
guard let searchText = searchBar.text, !searchText.isEmpty else {
return
}
searchBar.resignFirstResponder()
APIManager.shared.searchAnime(query: searchText, context: <#NSManagedObjectContext#>) { [weak self] (results, error) in
guard let self = self else { return }
if let results = results {
self.searchResults = results
DispatchQueue.main.async {
self.tableView.reloadData()
}
} else if let error = error {
print("Error searching for anime: \(error.localizedDescription)")
}
}
}
}