TableView with TableViewCell in UIViewController not showing the data from SearchBar

TableView with TableViewCell in UIViewController not showing the data from SearchBar

I am having trouble displaying the data I got from my REST API. I can retrieve the data with no problem but the tableview is not displaying the data. How do I get the data inside the tableview cell in the tableview?

I am having trouble displaying the data I got from my REST API. I can retrieve the data with no problem but the tableview is not displaying the data. How do I get the data inside the tableview cell in the tableview?

class SearchBSViewController: UIViewController {
    @IBOutlet weak var searchBar: UISearchBar!
    @IBOutlet weak var tableView: UITableView!

lazy var tapRecognizer: UITapGestureRecognizer = {
    var recognizer = UITapGestureRecognizer(target:self, action: #selector(dismissKeyBoard))
    return recognizer
}()

var searchResults: [Service] = []
let busStop = BusStop(odataMetadata: "", busStopCode: "", services: [])
let queryService = QueryService()

override func viewDidLoad() {
    super.viewDidLoad()
    alterLayout()
    searchBar.delegate = self
}

func alterLayout() {
    tableView.tableHeaderView = UIView()
    tableView.estimatedSectionHeaderHeight = 50
    navigationItem.titleView = searchBar
    searchBar.showsScopeBar = false
    searchBar.placeholder = "Search for bus stop by bus code"
}

}

extension SearchBSViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.searchResults.count }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell: BusCell = tableView.dequeueReusableCell(withIdentifier: "BusCell", for: indexPath) as? BusCell else {
        return UITableViewCell()
    }

    let bus = searchResults[indexPath.row]
    cell.busNoLbl.text = bus.serviceNo
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexpath: IndexPath) -> CGFloat {
    return 100
}

}

extension SearchBSViewController: UISearchBarDelegate { @objc func dismissKeyBoard() { searchBar.resignFirstResponder() }

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    dismissKeyBoard()
    print("Something")
    guard let searchText = searchBar.text, !searchText.isEmpty else { return }
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
    guard let searchNumber: Int = Int(searchText) else { return }
    queryService.GetBusStop(BusNo: searchNumber) {
        results in
        UIApplication.shared.isNetworkActivityIndicatorVisible  = false
        if let results = results {
            print(results.services?[1].nextBus?.estimatedArrival ?? 0)
            self.searchResults = results.services!
            self.tableView.reloadData()
            self.tableView.setContentOffset(CGPoint.zero, animated: false)
        }
    }
}

func position(for bar: UIBarPositioning) -> UIBarPosition {
    return .topAttached
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    view.addGestureRecognizer(tapRecognizer)
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    view.removeGestureRecognizer(tapRecognizer)
}

}

class BusCell: UITableViewCell { @IBOutlet weak var busNoLbl: UILabel! @IBOutlet weak var firstBusLbl: UILabel! @IBOutlet weak var secBusLbl: UILabel! @IBOutlet weak var thirdBusLbl: UILabel! @IBOutlet weak var firstPrg: UIProgressView! @IBOutlet weak var secPrg: UIProgressView! @IBOutlet weak var thirdPrg: UIProgressView! @IBOutlet weak var firstType: UILabel! @IBOutlet weak var secType: UILabel! @IBOutlet weak var thirdType: UILabel!

func configure(services: Service) {
    busNoLbl.text = services.serviceNo
    firstBusLbl.text = services.nextBus?.estimatedArrival
    secBusLbl.text = services.nextBus2?.estimatedArrival
    thirdBusLbl.text = services.nextBus3?.estimatedArrival
}

}

class QueryService { typealias QueryResult = (BusStop?) -> ()

var buses: BusStop = BusStop(odataMetadata: "", busStopCode: "", services: [])

let defaultSession = URLSession(configuration: .default)

var dataTask: URLSessionDataTask?

func GetBusStop(BusNo: Int, completionBlock: @escaping QueryResult){
    dataTask?.cancel()
    var urlComponents = URLComponents(string: "http://datamall2.mytransport.sg/ltaodataservice/BusArrivalv2")!
    urlComponents.queryItems = [URLQueryItem(name: "BusStopCode", value: String(BusNo))]
    guard let url = urlComponents.url else { return}

    let urlRequest = Header(url: url) //input the header for authorization

    dataTask = defaultSession.dataTask(with: urlRequest) { (data, response, error) in
        if error != nil {
            print(error!.localizedDescription)
        }

        guard let data = data else { return }
        do {
            let BusStopData = try
                JSONDecoder().decode(BusStop.self, from: data)

            DispatchQueue.main.async {
                print("Queue")
                completionBlock(BusStopData)
            }

        } catch let jsonError {
            print(jsonError)
        }
    }
    dataTask?.resume()

}

}


Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

iOS App Development: How Apple's iOS 13 & Swift 5 Will Change App Development Industry

Want to know how Apple's iOS 13 & Swift 5 will change the iOS app development industry? Refer to this blog that contains in-detail information on it.

How To Build Reminders iOS App in Swift 5 for Beginners

In this video, we build the Reminders App in Swift for iOS. Learn how to use dates, local / user notifications, tableviews, and much more. From scratch to working app, we'll go over every step up toe process together.

Creating an iOS app with user presence using Node.js and Swift

In this tutorial, we are going to see how you can add user presence to an iOS application using Pusher Channels, Node.js, and Swift.