簡體   English   中英

我的 TableView 數據源函數沒有被調用,我不知道為什么

[英]My TableView datasource functions aren't being called and I don't know why

所以我有一個 UITableView,你拖入視圖控制器的那種,而不是 tableviewController。 我在 viewDidLoad 中將委托屬性設置為 self:

    searchTableView.delegate = self
    searchTableView.isHidden = true
    searchTableView.isUserInteractionEnabled = false

我在故事板中添加了一個原型單元格,標識符為“SearchResultCell”。 這是我的 tableview 數據源函數:

extension MapViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    searchCompleter.queryFragment = searchBar.searchTextField.text!
    let completionResults = searchCompleter.results
    return completionResults.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    searchCompleter.queryFragment = searchBar.searchTextField.text!
    let completionResults = searchCompleter.results
    print(completionResults)

    let cell = tableView.dequeueReusableCell(withIdentifier: "SearchResultCell", for: indexPath)

    cell.textLabel?.text = completionResults[indexPath.row].subtitle

    return cell
}

}

從字面上看,這些函數中的任何代碼都沒有被調用。 (別擔心,isHidden 和 isUserInteractionEnabled 函數在我調用表視圖函數之前被切換,所以這不是問題)我想我沒有將這些函數連接到我的特定 UITableView,但我不知道還有什么除了將委托設置為我已經完成的自我之外。 任何關於如何讓這些被調用的建議都非常感謝!

這是我的視圖控制器的圖片,因此您可以查看缺少約束或定位是否有問題:

在此處輸入圖片說明

您還需要添加

searchTableView.dataSource = self

您還可以在故事板中設置兩個連接

數據源

但是,我認為你還有另一個問題——我認為你不能將 UITableView 嵌入到 MapViewController 中——這里有一個關於其他人有同樣問題的討論在 MapView 中實現 UITable

我認為您的解決方案是使用包含 Map 和 Table 的 UIViewController - 您可以使其看起來與您已有的相同。

確保completionResults.count不等於 0。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM