我收到了错误
致命错误:无法索引空缓冲区
当我加载视图后,我立即开始在我的集合视图中滚动。错误在cellForItemAtIndexPath
的第一行引发。
this question的答案给了我一些关于如何在加载数据之前不更新集合视图的想法,但有人可以向我解释为什么这甚至是必要的吗?如果posts
为空,则numberOfItemsInSection
应返回0,并且尚未调用cellForItemAtIndexPath
。
var posts:[Post] = []
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
getMyPosts()
}
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return posts.count as Int
}
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var p = posts[indexPath.row] // Chrashes here
// Dequeue cell and so on
}
func getMyPosts(){
self.posts = []
Alamofire.request(Api.Router.MyPosts).validate().responseObject{
(request, response, posts: PostCollection?, error) in
if error == nil {
self.posts = posts!.getPosts()
self.collectionView?.reloadData()
}
}
}
答案 0 :(得分:0)
我认为你不需要 override func numberOfSectionsInCollectionView(collectionView:UICollectionView) - > Int { 返回1 } 作为回报1
将返回0
您的案件不需要部分。