【发布时间】:2017-10-11 13:34:36
【问题描述】:
我得到一个 int 形式的数据索引。我正在获取我的数据的位置,可以是 1 或 2 或 3 或 4 .. 等等。我如何使用这些 int 从 collectionview 中获取 colletionviewcell。
在每次滚动时,我都会将单元格的索引设为 int,例如,最初下一次滑动时它将为 0,数字将为 1,下一次滑动编号将为 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)
}
}
【问题讨论】:
-
你想从哪里获取单元格?滚动?
-
在 scrollViewWillEndDragging 上是的
-
您无法检索单元格,因为它尚未出现在屏幕上。您能更具体地说明您要做什么吗?
-
更新了问题