Ios 拉刷新崩溃应用程序,因为索引超出范围
我在collection视图中实现了pull-to-refresh,我面临的问题是,我的应用程序将因索引外消息而崩溃。下面是我的cellForItem方法Ios 拉刷新崩溃应用程序,因为索引超出范围,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我在collection视图中实现了pull-to-refresh,我面临的问题是,我的应用程序将因索引外消息而崩溃。下面是我的cellForItem方法 override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueR
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CauNguyenCell
cell.postArray = postData[indexPath.item]
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return postData.count
}
我知道问题是因为我使用removeAll方法清除postData中的所有数据,但我必须这样做,以便我的数据数组将具有全新的数据
下面是我的刷新功能:
func handleRefresh(_ refreshControl: UIRefreshControl) {
refreshControl.beginRefreshing()
postData.removeAll()
fetchDataAgain()
self.collectionView.reloadData()
refreshControl.endRefreshing()
}
错误消息:线程1:致命错误:索引超出范围
我只是想问问有没有人对解决这个问题有什么建议。谢谢 请检查这些方法的
UICollectionViewDataSource
实现:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
func numberOfSections(in: UICollectionView)
在第一个示例中,您应返回当前项数
postData.count
,在您的示例中,第二个示例应返回1
,请检查UICollectionViewDataSource
实现中的以下方法:
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
func numberOfSections(in: UICollectionView)
在第一个示例中,您应该返回当前项数
postData.count
,在您的示例中,第二个示例应该返回1
我在我的项目中实现了相同的功能。首先,我在全局范围内创建了refreshControl实例,然后在视图控制器中的viewDidLoad()
方法调用中设置
var refreshControl : UIRefreshControl?
var arrWeddingInvitations = [MyModelClass]()
func initialSetup() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.tintColor = .yellow
self.refreshControl?.addTarget(self, action: #selector(getWeddingInvitations), for: .valueChanged)
self.cvWeddingInvitation.addSubview(refreshControl!)
}
这是从服务器获取数据的调用getweddingvitations()
方法
// This code will hide refresh controller
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
}
// This code in my API request completion block and will check responded array data, If it is not nil then assigned to my global array in view controller which are used to display data and reload collection view.
if arrInvitations != nil, arrInvitations.count > 0 {
self.lblEmptyData.isHidden = true
self.cvWeddingInvitation.isHidden = false
self.arrWeddingInvitations = arrInvitations
self.cvWeddingInvitation.reloadData()
} else {
self.lblEmptyData.isHidden = false
self.cvWeddingInvitation.isHidden = true
}
这是我当前项目中的工作代码。我希望这对你有帮助
请看以下视频:
我在我的项目中实现了同样的东西。首先,我在全局范围内创建了refreshControl实例,然后在视图控制器中的
viewDidLoad()
方法调用中设置
var refreshControl : UIRefreshControl?
var arrWeddingInvitations = [MyModelClass]()
func initialSetup() {
self.refreshControl = UIRefreshControl()
self.refreshControl?.tintColor = .yellow
self.refreshControl?.addTarget(self, action: #selector(getWeddingInvitations), for: .valueChanged)
self.cvWeddingInvitation.addSubview(refreshControl!)
}
这是从服务器获取数据的调用getweddingvitations()
方法
// This code will hide refresh controller
DispatchQueue.main.async {
self.refreshControl?.endRefreshing()
}
// This code in my API request completion block and will check responded array data, If it is not nil then assigned to my global array in view controller which are used to display data and reload collection view.
if arrInvitations != nil, arrInvitations.count > 0 {
self.lblEmptyData.isHidden = true
self.cvWeddingInvitation.isHidden = false
self.arrWeddingInvitations = arrInvitations
self.cvWeddingInvitation.reloadData()
} else {
self.lblEmptyData.isHidden = false
self.cvWeddingInvitation.isHidden = true
}
这是我当前项目中的工作代码。我希望这对你有帮助
请看以下视频:
按如下方式编辑代码:
func fetchDataAgain(completion: ((Bool) -> ())) {
// your code
if postData != nil, postData.count != 0 {
completion(true)
}
}
func handleRefresh(_ refreshControl: UIRefreshControl) {
refreshControl.beginRefreshing()
postData.removeAll()
fetchDataAgain { (complete) in
if complete {
self.collectionView.reloadData()
refreshControl.endRefreshing()
}
}
}
方法FetchDataReach将再次检查数组,如果零和计数!=处理程序中的0已完成重新加载数据。代码一步一步地执行所有操作,当您在集合中重新加载数据时,数组可以为空,也可以为零。通常,最好使用
处理程序如下编辑代码:
func fetchDataAgain(completion: ((Bool) -> ())) {
// your code
if postData != nil, postData.count != 0 {
completion(true)
}
}
func handleRefresh(_ refreshControl: UIRefreshControl) {
refreshControl.beginRefreshing()
postData.removeAll()
fetchDataAgain { (complete) in
if complete {
self.collectionView.reloadData()
refreshControl.endRefreshing()
}
}
}
方法FetchDataReach将再次检查数组,如果零和计数!=处理程序中的0已完成重新加载数据。
代码一步一步地执行所有操作,当您在集合中重新加载数据时,数组可以为空,也可以为零。最好使用处理程序
作为规则,不要在handleRefresh方法中重新加载UICollectionView的数据。再次获取FetchDataView()的数据后,重新加载UICollectionView。如果不起作用,请告诉我。请在FetchDataReach()响应时重新加载集合视图。在拉动刷新时,您正在从postData数组中删除数据。然后立即重新加载集合视图。在删除数据后,只需调用从数组postData中获取数据的函数或代码段即可。然后重新加载集合视图。希望您的问题能够得到解决。不要在HandlerRefresh方法中重新加载UICollectionView的数据。再次获取FetchDataView()的数据后,重新加载UICollectionView。如果不起作用,请告诉我。请在FetchDataReach()响应时重新加载集合视图。在拉动刷新时,您正在从postData数组中删除数据。然后立即重新加载集合视图。在删除数据后,只需调用从数组postData中获取数据的函数或代码段即可。然后重新加载集合视图。希望你的问题能得到解决。这就是我所拥有的。请你也发布FetchDataReach()代码和精确越界崩溃消息。这就是我所拥有的。请你也发布FetchDataReach()代码和精确越界崩溃消息。这是我的项目代码,你可以为你的,我的荣幸。这是我的项目代码,你可以为你的项目写同样的代码。@RaghibArshi,我的荣幸。