Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在返回视图时保留CollectionView单元格_Ios_Swift_Firebase_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Ios 如何在返回视图时保留CollectionView单元格

Ios 如何在返回视图时保留CollectionView单元格,ios,swift,firebase,uicollectionview,uicollectionviewcell,Ios,Swift,Firebase,Uicollectionview,Uicollectionviewcell,我有一个CollectionView,其中的单元格对应于Firebase数据库中的子级。我的问题是,当我切换到另一个视图,然后再切换回来时,CollectionView单元格就消失了 我有一个cellForItemAt函数: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell =

我有一个CollectionView,其中的单元格对应于Firebase数据库中的子级。我的问题是,当我切换到另一个视图,然后再切换回来时,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
}
我还有另一个代码snippit:

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])
}
但是,这给了我一个错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:节0中的项数无效。”。更新(3)后现有节中包含的项目数必须等于更新(3)前该节中包含的项目数,加上或减去从该节插入或删除的项目数(0插入,1删除),加上或减去移入或移出该节的项目数(0迁入,0迁出)。'

我还尝试使用此方法创建单元格:

let cell = playerCollectionView.dequeueReusableCell(withReuseIdentifier: "playerItem", for: IndexPath(item: i, section: 0)) as! PlayerCollectionViewCell

但是我得到了相同的错误。我如何在返回视图时重新创建单元格?

您想在视图中重新加载集合视图将出现方法检查您是否正在实现方法
集合视图(uu:numberOfItemsInSection:)
集合视图(u:cellForItemAt:)
UICollectionViewDataSource
对象中

当使用
cellForItem
访问单元格时,您应该检查是否实际获得了单元格。该方法仅返回可见单元格

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
中的返回值,以匹配新的项数

如果
playerArrayList
对象需要重新加载,您可以使用该生命周期方法

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // reload
}

你是如何返回的?你确定你没有前进到原始视图控制器的新实例吗?@Paulw11我正在切换的两个视图在同一堆栈中,因此我非常确定这不是一个新实例。这些分段本身是从视图控制器到视图控制器,而不是从按钮到视图控制器,因为我需要在分段之前执行操作。如果您没有使用“后退”按钮,则必须确保使用“展开”序列,否则会不断推送视图控制器的新实例。在
viewdiload
中放置日志语句或断点,如果多次调用该语句或断点,则创建新实例。@Paulw11我使用的是“后退”按钮,我使用的是print语句以确定viewDidLoad被多次调用。如何恢复第一个实例或在“后退”按钮上终止当前实例?我使用的是从主菜单视图到当前视图的显示序列,而不是按钮序列。我是否需要更改序列的种类?如果需要,是什么种类?我已添加了if语句来检查我是否接收到单元格,并且我已经添加了一个重载到ViewWillDisplay。我已经有了numberOfItemsInSection函数,但是我没有提到它,因为我不认为这是问题所在。好消息是我没有收到任何错误,但是现在有两个单元格。array playerArrayList只有3个项目,但是没有集合视图中有6个单元格。可能有什么问题?@MikaOK,我们已经到了一半:)我无法从这里提供的代码判断出可能有什么问题。可以查看cell.setupViews方法中的代码,并检查它是否清除了任何视图,或者之前对该方法的调用是否添加了这些视图。此外,搜索重复集合视图单元格的答案可能会提供单元格重复时的典型情况。
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // reload
}