【发布时间】:2016-11-24 03:35:42
【问题描述】:
我的代码:
DataService.dataService.fetchDataFromServer { (channel) in
self.channels.append(channel)
let indexPath = IndexPath(item: self.channels.count - 1, section: 0)
self.collectionView?.insertItems(at: [indexPath])
}
从服务器函数获取数据:
func fetchDataFromServer(callBack: @escaping (Channel) -> ()) {
DataService.dataService.CHANNEL_REF.observe(.childAdded, with: { (snapshot) in
let channel = Channel(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>)
callBack(channel)
})
}
项目数部分:
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of items
return 0
}
完整的错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 0 into section 0, but there are only 0 items in section 0 after the update'
我正在使用集合视图,但不知道为什么会出现此错误。
任何帮助将不胜感激!
【问题讨论】:
-
显示你的
numberOfItemsInSection方法 -
刚刚添加。去看看吧!
-
你返回 0。你可能想返回
self.channels.count -
确保
self.channels.count中有一些数据并且它的计数不是0 -
你为collection view设置了delegate和data source了吗?
标签: ios swift uicollectionviewcell indexpath