7

I have a searchController getting data from Google Places API. I am using a UITableView to populate the cells, however, they are not being populated (I assume due to the above error)

  • I have all the delegates set up
  • TableView is reloading data
  • The request is being printed in the console as expected

From what I have read on S/OF this could be something to do with the registration of the cell - see below code

TableView

func configureTableView() {
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor,
                         paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0,
                         width: 0, height: 0)
        tableView.tableFooterView = UIView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: recentSearchesCellIdentifier)
        tableView.register(ResultsCell.self, forCellReuseIdentifier: searchResultsCellIdentifier)
        tableView.keyboardDismissMode = .interactive
        tableView.backgroundColor = UIColor(red: 242/255, green: 242/255, blue: 243/255, alpha: 1)
    }

CellForItemAtIndexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let recentSearchesCell = tableView.dequeueReusableCell(withIdentifier: recentSearchesCellIdentifier, for: indexPath)
    let searchResultsCell = tableView.dequeueReusableCell(withIdentifier: searchResultsCellIdentifier, for: indexPath) as! ResultsCell
    switch sections[indexPath.section].section {
    case .RecentSearches:
        recentSearchesCell.selectionStyle = .none
        let recentSearches = recentResults[indexPath.row]
        recentSearchesCell.textLabel?.text = recentSearches
        recentSearchesCell.detailTextLabel?.text = recentSearches
        return recentSearchesCell
    case .SearchResults:
        searchResultsCell.selectionStyle = .none
        let result = searchResults[indexPath.row]
        searchResultsCell.result = result
        return searchResultsCell
    }
}

Cell

class ResultsCell: UITableViewCell {

    var result: GMSAutocompletePrediction? {
        didSet {
            self.textLabel?.attributedText = result?.attributedPrimaryText
            self.detailTextLabel?.attributedText = result?.attributedSecondaryText
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}
David Henry
  • 1,972
  • 20
  • 43

0 Answers0