集合视图单元格scrollToItem转换失败[Swift]

集合视图单元格scrollToItem转换失败[Swift],swift,uicollectionview,uicollectionviewcell,uicollectionviewlayout,Swift,Uicollectionview,Uicollectionviewcell,Uicollectionviewlayout,在我的swift应用程序中,我有一个包含很多项目的集合视图,我想滚动到一个项目,然后设置collectionview单元格自定义类的适当性,但是强制转换失败,为什么? 这是我的代码,我省略了一些明显的步骤,但结果是打印“Cast失败”: 类ViewController:UIViewController{ lazy var collectionView: UICollectionView = { let cv = UICollectionView(frame: self.view.boun

在我的swift应用程序中,我有一个包含很多项目的集合视图,我想滚动到一个项目,然后设置collectionview单元格自定义类的适当性,但是强制转换失败,为什么? 这是我的代码,我省略了一些明显的步骤,但结果是打印“Cast失败”:

类ViewController:UIViewController{

lazy var collectionView: UICollectionView = {
    let cv = UICollectionView(frame: self.view.bounds, collectionViewLayout: UICollectionViewFlowLayout())
    cv.backgroundColor = .gray
    cv.register(Cell.self, forCellWithReuseIdentifier: Cell.id)
    cv.delegate = self
    cv.dataSource = self
    return cv
}()
}

扩展视图控制器:UICollectionViewDelegate、UICollectionViewDataSource、UICollectionViewDelegateFlowLayout{

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Cell.id, for: indexPath) as! Cell
    cell.backgroundColor = .purple
    cell.label.text = "\(indexPath)"
    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if indexPath.item == 14 {
        collectionView.scrollToItem(at: IndexPath(item: 1, section: 0), at: .top, animated: true)
        guard let cell = collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as? Cell else {
            print("Cast fails")
            return
        }
        cell.label.text = "Something"
    }
}
}

以下是我找到的解决方案:

extension UICollectionView {
    func scrollToItem(at indexPath: IndexPath, at position: UICollectionView.ScrollPosition, completion: @escaping () -> ()) {
        scrollToItem(at: indexPath, at: .top, animated: true)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
            completion()
        }
    }
}

强制转换失败,因为(该特定索引的)单元格在查询时不可见

guard let cell = collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as? Cell else {
  print("Cast fails")
  return
} 
相反,您可以尝试设置
,at:.top,animated:false)
,或将上述代码片段包装到Dispatch after块中

顺便说一句,您最好更改数据源数组并重新加载该索引,这样您就不需要等待了

// change source array at that index
// scroll to that row with/without animation
// make sure you set value for the cell label in cellForRowAt table's datasource method 

强制转换失败,因为(该特定索引的)单元格在查询时不可见

guard let cell = collectionView.cellForItem(at: IndexPath(item: 1, section: 0)) as? Cell else {
  print("Cast fails")
  return
} 
相反,您可以尝试设置
,at:.top,animated:false)
,或将上述代码片段包装到Dispatch after块中

顺便说一句,您最好更改数据源数组并重新加载该索引,这样您就不需要等待了

// change source array at that index
// scroll to that row with/without animation
// make sure you set value for the cell label in cellForRowAt table's datasource method 

好的,为什么我不能使用performBatchUpdates而不是使用异步线程来完成任务?这是另一个选项。我不能更改源委托,因为我正在创建一个类似whatsapp的聊天,当你点击回复时,集合视图将显示原始消息,然后突出显示。好的,我已经发布了解决方案,谢谢你,Sn_Khan,0.5现在正是我的好时机,为什么我不能使用performBatchUpdates来完成,而不是使用异步线程?这是另一个选项尝试itI无法更改源委托,因为我正在创建一个类似whatsapp的聊天,当你点击回复时,集合视图将显示原始消息,然后突出显示它。确定,我已经发布了解决方案,谢谢Snu Khan,0.5这是我的最佳时机