如何将 CollectionView 单元格保留在 return 上以供查看
How to keep CollectionView cells on return to view
我有一个 CollectionView,它的单元格对应于 Firebase 数据库中的 children。我的问题是,当我转至另一个视图,然后转回时,CollectionView 单元格消失了。
我有一个 cellForItemAt 函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "playerItem", for: indexPath) as! PlayerCollectionViewCell
cell.setupViews(parentSize: Int(self.playerCollectionView.frame.width), hostUID: self.currentUID, tempUserName: playerArrayList[indexPath.item])
cell.layer.cornerRadius = 6
cell.layer.borderWidth = 2
return cell
}
我还有一个代码片段:
for i in 0..<playerArrayList.count{
print(playerCollectionView.numberOfItems(inSection: 0))
//playerCollectionView.insertItems(at: [IndexPath(item: i, section: 0)])
let cell = playerCollectionView.cellForItem(at: IndexPath(item: i, section: 0)) as! PlayerCollectionViewCell
cell.tempUserName = playerArrayList[i]
cell.setupViews(parentSize: Int(self.playerCollectionView.frame.width), hostUID: self.currentUID, tempUserName: playerArrayList[i])
}
我仍在学习 CollectionView 如何创建单元格,以及程序在 View 休眠时做什么,但我相信我的问题是 cellForItemAt 函数在第一次创建视图时创建了所有单元格,但没有之后不会再发生。
我确信这一点,因为当第一次创建视图时,有三个单元格(这正是数据库中有多少 children)但是当返回到同一个视图时,没有单元格.
我试图通过删除所有单元格然后重新制作它们来解决这个问题。
在代码片段中,您可以看到注释行:
playerCollectionView.insertItems(at: [IndexPath(item: i, section: 0)])
但是这给了我错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (3), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
我也试过用它来创建单元格:
let cell = playerCollectionView.dequeueReusableCell(withReuseIdentifier: "playerItem", for: IndexPath(item: i, section: 0)) as! PlayerCollectionViewCell
但是我得到了同样的错误。如何将 return 上的单元格重新创建到视图?
您想在 viewWillAppear 方法中重新加载集合视图
检查您是否在 UICollectionViewDataSource
对象中实现方法 collectionView(_:numberOfItemsInSection:)
和 collectionView(_:cellForItemAt:)
。
当使用 cellForItem
访问单元格时,你应该检查你是否真的得到了一个单元格。该方法仅 returns 可见单元格。
if let cell = self.playerCollectionView.cellForItem(at: indexPath) as? PlayerCollectionViewCell {
cell.tempUserName = playerArrayList[i]
cell.setupViews(parentSize: Int(self.playerCollectionView.frame.width), hostUID: self.currentUID, tempUserName: playerArrayList[i])
}
不是再次插入单元格,您应该有一个集合视图委托,告诉有多少项目要显示
override function viewDidLoad() {
super.viewDidLoad()
playerCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return playerArrayList.count
}
insertItemsAt
方法要求您从 numberOfItemsInSection
更新 return 值以匹配新的项目数。
如果 playerArrayList
对象需要重新加载,您可以使用该生命周期方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// reload
}
我有一个 CollectionView,它的单元格对应于 Firebase 数据库中的 children。我的问题是,当我转至另一个视图,然后转回时,CollectionView 单元格消失了。
我有一个 cellForItemAt 函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "playerItem", for: indexPath) as! PlayerCollectionViewCell
cell.setupViews(parentSize: Int(self.playerCollectionView.frame.width), hostUID: self.currentUID, tempUserName: playerArrayList[indexPath.item])
cell.layer.cornerRadius = 6
cell.layer.borderWidth = 2
return cell
}
我还有一个代码片段:
for i in 0..<playerArrayList.count{
print(playerCollectionView.numberOfItems(inSection: 0))
//playerCollectionView.insertItems(at: [IndexPath(item: i, section: 0)])
let cell = playerCollectionView.cellForItem(at: IndexPath(item: i, section: 0)) as! PlayerCollectionViewCell
cell.tempUserName = playerArrayList[i]
cell.setupViews(parentSize: Int(self.playerCollectionView.frame.width), hostUID: self.currentUID, tempUserName: playerArrayList[i])
}
我仍在学习 CollectionView 如何创建单元格,以及程序在 View 休眠时做什么,但我相信我的问题是 cellForItemAt 函数在第一次创建视图时创建了所有单元格,但没有之后不会再发生。 我确信这一点,因为当第一次创建视图时,有三个单元格(这正是数据库中有多少 children)但是当返回到同一个视图时,没有单元格. 我试图通过删除所有单元格然后重新制作它们来解决这个问题。 在代码片段中,您可以看到注释行:
playerCollectionView.insertItems(at: [IndexPath(item: i, section: 0)])
但是这给了我错误:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (3) must be equal to the number of items contained in that section before the update (3), plus or minus the number of items inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'
我也试过用它来创建单元格:
let cell = playerCollectionView.dequeueReusableCell(withReuseIdentifier: "playerItem", for: IndexPath(item: i, section: 0)) as! PlayerCollectionViewCell
但是我得到了同样的错误。如何将 return 上的单元格重新创建到视图?
您想在 viewWillAppear 方法中重新加载集合视图
检查您是否在 UICollectionViewDataSource
对象中实现方法 collectionView(_:numberOfItemsInSection:)
和 collectionView(_:cellForItemAt:)
。
当使用 cellForItem
访问单元格时,你应该检查你是否真的得到了一个单元格。该方法仅 returns 可见单元格。
if let cell = self.playerCollectionView.cellForItem(at: indexPath) as? PlayerCollectionViewCell {
cell.tempUserName = playerArrayList[i]
cell.setupViews(parentSize: Int(self.playerCollectionView.frame.width), hostUID: self.currentUID, tempUserName: playerArrayList[i])
}
不是再次插入单元格,您应该有一个集合视图委托,告诉有多少项目要显示
override function viewDidLoad() {
super.viewDidLoad()
playerCollectionView.dataSource = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return playerArrayList.count
}
insertItemsAt
方法要求您从 numberOfItemsInSection
更新 return 值以匹配新的项目数。
如果 playerArrayList
对象需要重新加载,您可以使用该生命周期方法
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// reload
}