【问题标题】:Display city names in a TableView在 TableView 中显示城市名称
【发布时间】:2020-06-22 07:17:11
【问题描述】:

我想根据用户在搜索栏中输入的内容在表格视图上显示所有城市名称。 例如,如果用户在 searchBar 中键入“New York”,我希望显示 API 给我的所有结果。 我正在使用返回 JSON 数据的 API。

现在我正在使用这个委托方法:

extension searchViewController : UISearchBarDelegate{

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

        RequestAPI.queryCities(cityNameString: searchBar.text!) { (cities) in
        for iterator  in 0...cities.geonames.count-1 {
            self.citiesQueried.append(cities.geonames[iterator])
        }
    }

        self.tableView.reloadData()
}


func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    print("cancel button clicked")
    self.citiesQueried.removeAll()
    self.tableView.reloadData()
}
}

citiesQueried 是我之前在 viewController 中声明为空的数组,它的任务是收集 API 请求产生的所有数据。

这些是 UITableView 的委托方法:

extension searchViewController : UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return citiesQueried.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCityTableViewCell", for: indexPath)
    cell.textLabel?.text = self.citiesQueried[indexPath.row].name
    return cell
}
}

问题是它有时有效,有时无效,我需要按两次搜索按钮才能看到 tableView 上显示的城市。 我认为缺少一些东西,我希望你们中的一些人能给我一些关于如何解决这个问题的建议

【问题讨论】:

  • 从 API 成功重新加载您的 tableView。
  • 您需要在queryCities 的完成处理程序闭包中调用reloadData,并记住将其分派到主队列中

标签: ios swift api uitableview uisearchbar


【解决方案1】:

您必须在异步任务的完成处理程序中重新加载表格视图

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    RequestAPI.queryCities(cityNameString: searchBar.text!) { (cities) in
        for iterator  in 0..<cities.geonames.count {
            self.citiesQueried.append(cities.geonames[iterator])
        }
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

旁注:

名称cities 暗示是一个数组,但它似乎是一个对象。则不需要for循环,并以单数形式命名参数

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {

    RequestAPI.queryCities(cityNameString: searchBar.text!) { city in          
        self.citiesQueried = city.geonames         
        DispatchQueue.main.async {
            self.tableView.reloadData()
        }
    }
}

【讨论】:

  • 非常感谢,这个建议对我帮助很大,是的,实际上城市不是一个数组,而是只有一个变量是一个城市数组,所以我决定称之为城市
  • 非常感谢,我不知道为什么searchBarCancelButtonClicked委托方法不起作用,我想删除所有查询的城市并重新加载tableView但它似乎没有响应在所有的用户输入中
  • 是的,该方法被调用并且是我在我的问题中包含的 sn-p,但我不知道为什么当我按下取消按钮时没有任何反应,甚至括号内的 print() 也没有/跨度>
【解决方案2】:

从 API 成功后重新加载 tableView,如下所示:

RequestAPI.queryCities(cityNameString: searchBar.text!) { (cities) in
         for iterator  in 0...cities.geonames.count-1 {
            self.citiesQueried.append(cities.geonames[iterator])
         }
      self.tableView.reloadData()
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-07
    相关资源
    最近更新 更多