0

I've worked through a lot of answers like this one but I'm still stuck.

I have a viewcontroller with a collectionView and a searchController. When the user searches I want to display a UITableView on top of the collectionView. I'm trying to add the tableview programmatically b/c it's a simple text label I'm displaying

I am able to display the search results for the dummy array I have in viewdidload of my tableviewcontroller class. However when I get the actual results, I can only print them in the numberOfRows function.

All help would be greatly appreciated, here's my code:

This is my TableView Class:

import UIKit

class HashtagSearchList: UITableViewController {

    var hashtagListTableview = UITableView()
    var hashtagList = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        hashtagList = ["One", "Two", "Three"]
        hashtagListTableview.delegate = self
        hashtagListTableview.dataSource = self
    }

    // MARK: - Table view data source

    //I don't need this, but added it based on some answers... didn't help
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        //All these print statements show correct values
        print("List Count = \(hashtagList.count)")
        print("List of Strings: \(hashtagList)")
        print("frame size = \(tableView.frame)")
        return hashtagList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        //This print statement only fires on ViewdidLoad
        print("cellForRowAt = \(hashtagList[indexPath.row])")
        cell.textLabel?.text = hashtagList[indexPath.row]
        cell.backgroundColor = .red

        return cell
    }
}

Here is my ViewController / SearchController code:

class ExploreVC: UIViewController {

    var hashtagSearchController = HashtagSearchList()

    override func viewDidLoad() {
        super.viewDidLoad()
        //Search Controller
        navigationItem.searchController = searchController
        searchController = UISearchController(searchResultsController: hashtagSearchController)
    }

    //......

    // SEARCH CONTROLLER

    extension ExploreVC: UISearchResultsUpdating {
        func updateSearchResults(for searchController: UISearchController) {
            let searchText = searchController.searchBar.text

            if searchText == "" { return }

            PostFirebase.getHashtagList(hashtag: searchText) { (hashtagList) in

                self.hashtagSearchController.hashtagListTableview.frame = CGRect(x: 0.0, y: 0.0, width: self.view.bounds.width, height: self.view.bounds.height)

                self.hashtagSearchController.hashtagList = hashtagList
                //self.hashtagSearchController.hashtagListTableview.reloadData()

                DispatchQueue.main.async {
                    self.hashtagSearchController.hashtagListTableview.reloadData()
                }
            }
        }
    }
}

Here is the tableview from viewDidLoad, and it never changes from here enter image description here

Raju Abe
  • 735
  • 2
  • 10
  • 25
TJMac
  • 129
  • 1
  • 12
  • Check out the frame of the tableview which is not reloading. Tableview will reload only those cells which are currently visible in frame. – Jitendra Tanwar Feb 14 '20 at 05:06

1 Answers1

0

You initialized hashtagListTableview in HashtagSearchList but you didn't add layout constraints. By default, it will have .zero frame and won't be displayed on the screen.

I guess that the table view on the screen is tableView from UITableViewController. That's why nothing happened when you call self.hashtagSearchController.hashtagListTableview.reloadData().

To fix it, try to use tableView instead of hashtagListTableview. Replace

self.hashtagSearchController.hashtagListTableview.reloadData()

with

self.hashtagSearchController.tableView.reloadData()
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • Thank you for the catch! I had added some constraints, and manually set the frame size before with no luck, but now it makes sense why, I am actually using a different tableview... did not think about that. Thanks again, made my day :) – TJMac Feb 14 '20 at 17:06