Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/101.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 数据源未更新单元格_Ios_Swift - Fatal编程技术网

Ios 数据源未更新单元格

Ios 数据源未更新单元格,ios,swift,Ios,Swift,当我试图从相应ViewController中的闭包更新数据源(在单独的类中是自定义的)时,它将不起作用 这就是我用来更新数据源的代码 extension YelpSearchController: UISearchResultsUpdating { func updateSearchResults(for searchController: UISearchController) { guard let searchTerm = searchController.searchBar.t

当我试图从相应ViewController中的闭包更新数据源(在单独的类中是自定义的)时,它将不起作用

这就是我用来更新数据源的代码

extension YelpSearchController: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {

    guard let searchTerm = searchController.searchBar.text else {
        return
    }

    let yelpCoordinate = YLPCoordinate(latitude: 37.785834000000001, longitude: -122.406417)
    let yelpClient = YLPClient.init(apiKey: appSecret)
    yelpClient.search(with: yelpCoordinate, term: searchTerm , limit: 30, offset: 1, sort: YLPSortType.distance) { [weak self] result, error in
        guard let results = result else { return }
        let businesses = results.businesses
        self?.dataSource.update(with: businesses)           
    }
  }
}
这个函数只是在datasource类中更新我的数据变量,如下所示:

private var data = [YLPBusiness]()
func update(with data: [YLPBusiness]) {
    self.data = data
}
问题是,当我调用委托方法(作为数据源所需)时,它们最初会被调用,但当它们被调用时,数据变量仍然没有更新,因此数据为零

例如,如果我尝试在update func中打印data.count,就会得到一个结果。但在任何委托方法(cellForRowAt、numberOfRowsInSection)中,都是零。因此,除了更新函数之外,所有涉及和使用数据变量的方法都没有得到数据,因为视图正在加载,但是当用户在搜索字段中输入数据时,数据在后面


因此,问题是如何获取委托方法可访问的数据?

在从闭包内部接收数据后忘记更新数据源

yelpClient.search(with: yelpCoordinate, term: searchTerm , limit: 30, offset:    1, sort: YLPSortType.distance) { [weak self] result, error in
        guard let results = result else { return }
        let businesses = results.businesses
            self?.dataSource.update(with: businesses)
        DispatchQueue.main.async {. // added this 
            self?.tableView.reloadData()
        }

非常感谢DonMag给出正确答案。

收到数据后是否重新加载tableview?@DonMag Dude!!!你是最棒的。这么愚蠢的错误,我刚刚添加了:DispatchQueue.main.async{self?.tableView.reloadData()}现在一切都正常了!谢谢