我有一个searchController从Google Places API获取数据。我正在使用UITableView填充单元格,但是并未填充它们(由于上述错误,我认为是这样)
根据我在S / OF上阅读的内容,这可能与单元格的注册有关-请参见下面的代码
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)
}
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
}
}
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
}
}