fatal error: Index out of range when refreshing Collection View

fatal error: Index out of range when refreshing Collection View

我在 UICollectionView 中添加了一个刷新按钮,但我尝试了很多方法后还是出现了同样的错误。

UICollectionView 工作 fine.pull 刷新也工作正常但如果我反复拉我会收到此错误。

致命错误:索引超出范围

该功能在完全完成之前应该不会再次运行?

我该如何解决这个问题?

    func veriCek(){

        var tempOrdersList = [HomeFeedVideo]()

        self.posts.removeAll()
        tempOrdersList.removeAll()

        Alamofire.request("**************************", method: .post, parameters: parameters).validate().responseJSON { response in
            switch response.result {
            case .success:
                //print(response.data!)
                if let result = response.result.value {
                    let json = result as! NSDictionary
                    //print(json)

                    let dataArray = json["sonuclar"] as! NSArray;

                    print("Data items count: \(dataArray.count)")
                    for item in dataArray { // loop through data items

                       // ... some code

                        let post = HomeFeedVideo(artistName: artistName, commentlikes: commentlikes, likeStatus: likeStatus, name: name, profilePics: profilePics, time: time, userID: userID, username: username, videoContent: videoContent, videoID: videoID, videoLikes: videoLikes, videoName: videoName, videoPath: videoPath, view: view, status: groupStatus, groupResponse: groupRes, thumbnail: thumbnail)
                        tempOrdersList.append(post)

                    }

                    DispatchQueue.main.async {
                        self.posts = tempOrdersList
                        self.user = User(dictionary: json as! [String : Any])
                        self.navigationItem.title = self.user?.username
                        self.playerCollect?.reloadData()
                        self.refreshControl.endRefreshing()
                    }
                }
            case .failure(let error):
                print(error)
            }
        }        
    }

我认为您在 collectionView 数据源方法中遇到了错误 cellForItemAt(indexPath:) 方法。您可能试图在删除数据时设置单元格 post。如果您分享您的数据源实现,我也许能找到问题所在。仅当您的数据不为空时才尝试设置 post。

在 cellForItemAt 方法中添加此检查:-

if !posts.isEmpty {
// TODO: - Set your cell's post
}