我有一个水平分页启用的UICollectionView,每个单元格填充整个屏幕并包含一个ImageView。
我想要的是更新两个正在分页的单元格(如果您从0页转到1页,我想在中间更新它们)。
因此,我夸大了以下方法:
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
let pageWidth = collectionView.frame.size.width
var currentPage = collectionView.contentOffset.x / pageWidth
if (0.0 != fmodf(Float(currentPage), 1.0)) {
currentPage += 1
}
let indexPath = IndexPath(row: Int(currentPage), section: 0)
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as? MyCell else {
return
}
// This cell is moving out of screen
cell.image.isHidden = true
}和
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageWidth = collectionView.frame.size.width
var currentPage = collectionView.contentOffset.x / pageWidth
if (0.0 != fmodf(Float(currentPage), 1.0)) {
currentPage += 1
}
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: IndexPath(row: Int(currentPage), section: 0)) as? MyCell else {
return
}
// This cell entered the screen
cell.image.isHidden = false
}问题是,图像隐藏状态的任何更改都不会生效,如果我打印cell.image.isHidden,它将显示正确的状态,但ui没有更新。
发布于 2018-10-25 13:45:31
替换每一个
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath) as? MyCell else {
return
}使用
guard let cell = collectionView.cellForItem(at:indexPath) as? MyCell else {
return
}https://stackoverflow.com/questions/52990434
复制相似问题