7

我有一个 searchController 从 Google Places API 获取数据。我正在使用 UITableView 填充单元格,但是,它们没有被填充(我假设由于上述错误)

  • 我已经设置了所有代表
  • TableView 正在重新加载数据
  • 请求正在按预期打印在控制台中

根据我在 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)
    }

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
    }
}

细胞

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
    }
}
4

0 回答 0