【发布时间】:2018-02-07 08:10:10
【问题描述】:
有一个函数可以更新我的 UICollectionView:
@objc func refreshFeed(sender:AnyObject)
{
fetchWeatherData()
}
private func fetchWeatherData() {
PostArray = [PostModel]()
offset = 0
self.fetchPost()
self.refreshControl.endRefreshing()
}
此函数导致 PostArray 集合无效并调用 fetchPost 功能区更新函数:
func fetchPost(URL: String){
ApiService.sharedInstance.fetchPost(url: URL, completion: { (Posts: [PostModel]) in
self.PostArray.append(contentsOf: Posts)
self.collectionView.reloadData()
})
}
更新集合后,函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! PostCell
cell.PostArray = PostArray[indexPath.item]
return cell
}
给出错误消息:Thread 1: Fatal error: Index out of range in line:cell.PostArray = PostArray[indexPath.item]
为什么会这样?数据落入集合中。
对不起我的英语
【问题讨论】:
-
你在
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int返回什么? -
建议您使用
cell.PostArray = PostArray[indexPath.row]作为类名而不是实例名的大写字母名称 -
@GregoryHigley
let count = self.PostArray.count return count -
这是正确的
cell.PostArray = PostArray否则
标签: ios arrays swift uicollectionview