Swift 使用scrollViewDidScroll处理多个集合视图单元格
我目前正在使用Swift 使用scrollViewDidScroll处理多个集合视图单元格,swift,uicollectionview,uiscrollview,uicollectionviewcell,Swift,Uicollectionview,Uiscrollview,Uicollectionviewcell,我目前正在使用scrollViewDidScroll设置我的UICollectionView单元格偏移量的动画。集合视图以前只包含1UICollectionViewCell 我刚刚在同一个集合视图中添加了另一个UICollectionViewCell,并希望执行相同的动画。问题是,我遇到了一个错误崩溃: Precondition failed: NSArray element failed to match the Swift Array Element type. Expected Detai
scrollViewDidScroll
设置我的UICollectionView
单元格偏移量的动画。集合视图以前只包含1UICollectionViewCell
我刚刚在同一个集合视图中添加了另一个UICollectionViewCell
,并希望执行相同的动画。问题是,我遇到了一个错误崩溃:
Precondition failed: NSArray element failed to match the Swift Array Element type. Expected DetailedCell but found BasicCell
我的进度:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// First (Detailed Cell)
for newDetailedCell in mainCV.visibleCells as! [DetailedCell] { // Crashes on this line
let indexPath = mainCV.indexPath(for: newDetailedCell)
print("indexPath for Detailed Cell: \(indexPath)")
}
// Rest (Basic Cell)
for basic in mainCV.visibleCells as! [BasicCell] {
let indexPath = mainCV.indexPath(for: basic)
print("indexPath for Basic Cell: \(indexPath)")
}
}
如何使用两个不同的集合ViewCells访问visibleCells?您可以试试
for cell in mainCV.visibleCells {
if let res = cell as? DetailedCell {
//
}
else
if let res = cell as? BasicCell {
//
}
}
或
要处理多个
UICollectionView
的滚动事件,请在scrollViewDidScroll
中执行此条件:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == collectionView1 {
// Your code goes here
}
if scrollView == collectionView2 {
// Your code goes here
}
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == collectionView1 {
// Your code goes here
}
if scrollView == collectionView2 {
// Your code goes here
}
}