Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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
Arrays 在集合视图中使用转义闭包中的数组_Arrays_Swift_Uicollectionview_Escaping_Closures - Fatal编程技术网

Arrays 在集合视图中使用转义闭包中的数组

Arrays 在集合视图中使用转义闭包中的数组,arrays,swift,uicollectionview,escaping,closures,Arrays,Swift,Uicollectionview,Escaping,Closures,首先,请原谅任何错误的术语,我几周前才开始学习转义闭包 我有一个API在转义闭包中返回一个数组。函数的调用方式如下: getAllUserMovies(username: user) { (result) in switch result { case .success(let movies): // movies is an array. Do something with each element

首先,请原谅任何错误的术语,我几周前才开始学习转义闭包

我有一个API在转义闭包中返回一个数组。函数的调用方式如下:

getAllUserMovies(username: user) { (result) in
            switch result {
            case .success(let movies):
                // movies is an array. Do something with each element
                break
            case .error(let error):
                // report error
                break
            }
}
我需要使用该数组的元素,每次只使用一个,在这个集合视图方法中,它实际上比这个更复杂,因为我还与TMDB API接口:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
我尝试以类似的方式使用嵌套闭包:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

            let cell : collectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: self.reuseIdentifierLargeBanners, for: indexPath as IndexPath) as! CollectionViewCell

            getAllUserMovies(username: user){ (result) in
                switch result {
                case .success(let movies):
                    let movie = movies[indexPath.row] {
                        cell.imageView.image = movie.poster
                    }
                    break
                case .error(let error):
                    print(error?.localizedDescription ?? "")
                    break
                }
            }
            return cell
        }
        return UICollectionViewCell()
    }
基本上我有两个问题。首先,我认为这是完全没有效率的,我必须在我刷新一个单元格时获取完整的电影列表,然后我也会得到有趣的结果,比如重复的横幅,它保持刷新、移动或丢失图标。我实现了这一公认的答案,但仍然无法使其发挥作用。无论我做什么,我要么得到重复的图像,空单元格,要么两者都有

更新:缺少图标似乎是由于API中每秒调用数的限制。超过这个数字,API就会失败,我没有检查错误


我认为一个可能的解决方案是将movies数组存储在某个地方,然后能够从collection-view方法中从中获取单个的movies。如果需要,请刷新它。感谢您的回答,现在已经修复了

如果每次滚动时都在CellForItem中实现API调用,将导致API调用

我建议您在didLoad中进行API调用,或者在全局保留结果后显示并刷新collection视图

func makeAPICall() {
getAllUserMovies(username: user){ (result) in
                switch result {
                case .success(let movies):
                    self.movies = movies
                    yourCollectionView.reloadData()
                    break
                case .error(let error):
                    print(error?.localizedDescription ?? "")
                    break
                }
            }
}
集合视图数据源

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.movies.count
    }

重复出现的问题是因为单元格被重用。如果出现错误,或者在加载过程中,不执行cell.imageView.image=nil以删除可能的前一个图像。另外,只需使用var movies:[MovieClass],并在某个点init调用getAllUserMovies{}?一旦你得到ti,设置电影并重新加载tableView。我已经在其他问题中读到人们建议重新加载tableView。但我没有任何桌面视图。UICollectionView是否也有重新加载?我之前检查过,但找不到如何实现它。我是指UICollectionView。那是同样的行为。我看看我能从这里走到哪里。谢谢。这就是我在没有定义函数的情况下测试的,我已经在didLoad中有了它,添加了重载数据,但是在func collectionView之后,didLoad被执行了??这给出了一个运行时错误线程1:致命错误:let movieID=self.movies[indexPath.row].movie?.ids.tmdb上的索引超出范围。您为numberOfRow委托返回了什么?我没有。我从互联网上取了一个样本作为基础。它与本地图像一起工作。然后,我添加了框架,包括转义闭包和所有内容,我的噩梦开始了。无论如何,didLoad显然会在任何操作之前被调用,但是collectionView会在getAllUserMovies返回其值之前访问数组。对不起,错了。在NumberOfItems部分,您应该返回电影。countOMG!!太美了!它现在起作用了!!好的,我有765个细胞,但谁在计数我会放一个if。我的另外两个收藏视图也消失了,但我想我可以很容易地从这里继续。谢谢