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

}

}


ios swift

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup? **[Hire Dedicated Swift Developers](https://hourlydeveloper.io/hire-dedicated-swift-developer/ "Hire Dedicated Swift Developers")** for end-to-end services like development, migration...

Hire iOS App Developer

Are you looking to transform your idea into an iPhone application? Hire iPhone programmer team from **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** to ensure the best results, utilizing all the latest trends in iOS app...

HOW TO BUILD CUSTOM ALERT DIALOG BOX FROM SCRATCH STEP BY STEP USING SWIFT iOS XCODE 11.4

Topic: HOW TO BUILD CUSTOM ALERT DIALOG BOX FROM SCRATCH STEP BY STEP USING SWIFT iOS XCODE 11.4 Subscribe Us: http://bit.ly/2UaSC5s Find us on: Discord: htt...

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.

Easy way to save the class object in user defaults - Swift iOS

Easy way to save the class object in user defaults - Swift iOS Text tutorial: http://adnan-tech.com/easy-way-to-save-the-class-object-in-user-defaults-swift-...