我得到的数据索引为int。我得到我的数据的位置可以是1或2或3或4 ..等等。我如何使用这些int从collectionview中获取colletionviewcell。Swift - 如何从UICollectionView中获取基于整数索引位置的UICollectionViewCell
在每个滚动我得到单元格的索引作为int例如初始它将为0下一次刷卡的数字将是1,下一次刷卡nos将是2等等。
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
let cellWidthIncludingSpacing = layout.itemSize.width + layout.minimumLineSpacing
var offset = targetContentOffset.pointee
let index = (offset.x + scrollView.contentInset.left)/cellWidthIncludingSpacing
let roundIndex = round(index)
offset = CGPoint(x: roundIndex * cellWidthIncludingSpacing - scrollView.contentInset.left, y: -scrollView.contentInset.top)
targetContentOffset.pointee = offset
for cell in collectionView.visibleCells as! [CaurosalCollectionViewCell] {
// do something
print(cell.title.text)
}
}
要获取细胞在哪里?在滚动? – ahbou
yes在scrollViewWillEndDragging –
您无法检索该单元,因为它尚未显示在屏幕上。你能更具体地说明你想做什么? –