Hello everyone, I go through the lessons without using a storyboard, sometimes I have difficulties because the code is different from what the teacher does. I ran into a problem, my filter in textField does not work, although I found the answer to the problem here on the forum. Can anyone help me solve the problem? There are no errors, but the filter does not work.
SceneDelegate:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let myWindow = UIWindow(windowScene: windowScene)
let navigationController = UINavigationController()
let viewController = MainTabBarController()
navigationController.viewControllers = [viewController]
myWindow.rootViewController = navigationController
self.window = myWindow
myWindow.makeKeyAndVisible()
}
}
MainViewController:
class MainViewController: UITableViewController {
private let identifier = "identifier"
var petitions = [Petition]()
var filteredPetitions = [Petition]()
override func viewDidLoad() {
super.viewDidLoad()
setupTableView()
let urlString: String
if navigationController?.tabBarItem.tag == 0 {
urlString = "https://www.hackingwithswift.com/samples/petitions-1.json"
} else {
urlString = "https://www.hackingwithswift.com/samples/petitions-2.json"
}
if let url = URL(string: urlString) {
if let data = try? Data(contentsOf: url) {
parseJSON(json: data)
filteredPetitions = petitions
return
}
}
showError()
}
func parseJSON(json: Data) {
let decoder = JSONDecoder()
if let jsonPetitions = try? decoder.decode(Petitions.self, from: json) {
petitions = jsonPetitions.results
tableView.reloadData()
}
}
private func showError() {
let ac = UIAlertController(title: "Loading error",
message: "There was a problem loading the feed; please check you connection and try again",
preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "OK",
style: .default))
present(ac, animated: true)
}
private func setupTableView() {
tableView.register(UITableViewCell.self,
forCellReuseIdentifier: identifier)
}
}
//MARK: TableView Delegate
extension MainViewController {
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return filteredPetitions.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: identifier,
for: indexPath)
let petition = petitions[indexPath.row]
cell.textLabel?.text = petition.title
cell.detailTextLabel?.text = petition.body
return cell
}
}
MainTabBarController:
class MainTabBarController: UITabBarController {
var mainVC = MainViewController()
override func viewDidLoad() {
super.viewDidLoad()
setupTabBar()
setupItems()
setupNavItem()
}
private func setupNavItem() {
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .search,
target: self,
action: #selector(filterTapped))
}
@objc func filterTapped() {
let ac = UIAlertController(title: "Enter:",
message: nil,
preferredStyle: .alert)
ac.addTextField()
let submitAction = UIAlertAction(title: "Submit",
style: .default) { [weak self, weak ac] action in
guard let answer = ac?.textFields?[0].text else { return }
self?.submit(answer)
}
ac.addAction(submitAction)
present(ac, animated: true)
}
func submit(_ answer: String) {
mainVC.filteredPetitions.removeAll(keepingCapacity: true)
for x in mainVC.petitions {
if x.title.contains(answer){
mainVC.filteredPetitions.append(x)
mainVC.tableView.reloadData()
}
}
}
private func setupTabBar() {
tabBar.layer.borderWidth = 0.5
}
private func setupItems() {
let mainViewController = MainViewController()
let vc = MainViewController()
setViewControllers([mainViewController, vc], animated: true)
mainViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 0)
vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
}
}
PetitionModel:
struct Petitions: Codable {
var results: [Petition]
}
struct Petition: Codable {
var title: String
var body: String
var signatureCount: Int
}