繁体   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