致命错误:刷新集合视图时索引超出范围

时间:2019-09-15 22:09:04

标签: swift uicollectionview fatal-error pull-to-refresh indexoutofrangeexception

我添加了一个拉动以刷新到UICollectionView,但是即使我尝试了很多事情,我仍然遇到相同的错误。

UICollectionView可以正常工作。拉动刷新也可以,但是如果我反复拉动,则会收到此错误。

致命错误:索引超出范围

该功能在完全完成之前不应该再次工作?

我该如何解决这个问题?

    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)
            }
        }        
    }

1 个答案:

答案 0 :(得分:1)

我认为您在collectionView数据源方法中遇到错误 cellForItemAt(indexPath :)方法。您可能正在尝试在删除数据时设置单元格帖子。如果您共享dataSource的实现,我也许可以找到问题。仅当您的数据不为空时,才尝试设置发布。

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

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