【发布时间】:2018-03-25 05:18:56
【问题描述】:
我想为选定的单元格添加复选标记。如果随后取消选择单元格(因此选择了另一个单元格),我想删除复选标记。顶部图像视图独立于集合视图,代表用户相机胶卷中选定的单元格。集合视图的数据源是从 PHAsset 中获取的图像列表。
问题是,如果我不在 indexPath 处重新加载项目,则取消选中的单元格复选标记不会被删除。此外,如果我滚动浏览集合视图,我会看到我什至没有点击的单元格中的随机复选标记。我假设它与细胞的重复使用有关。谁能帮帮我?
下面是视图控制器和单元格中我的代码的 sn-ps...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoCellReuseIdentifier, for: indexPath) as? PhotosCell else { fatalError("Couldn't return a photo cell") }
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.isNetworkAccessAllowed = true
requestOptions.deliveryMode = .highQualityFormat
photoManager.requestImage(for: photoAssets![indexPath.row], targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFill, options: requestOptions, resultHandler: { (image, nil) in
cell.photoView.image = image
})
if cell.isSelected {
cell.setupCheckmarkBanner()
}
else {
cell.removeCheckmarkBanner()
}
if indexPath.row == 0 && self.isFirstLoad {
self.selectedImageView.image = cell.photoView.image
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: UICollectionViewScrollPosition.bottom)
cell.setupCheckmarkBanner()
self.isFirstLoad = false
}
return cell
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
previousIndexPath = indexPath
collectionView.reloadItems(at: [indexPath])
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if previousIndexPath != indexPath {
collectionView.reloadItems(at: [indexPath])
guard let cell = collectionView.cellForItem(at: indexPath) as? PhotosCell else { fatalError("Couldn't return a photo cell") }
selectedImageView.image = cell.photoView.image
}
}
class PhotosCell: UICollectionViewCell {
let photoView = UIImageView()
var checkmarkBannerImageView: UIImageView?
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(photoView)
photoView.anchor(top: topAnchor, left: leadingAnchor, bottom: bottomAnchor, right: trailingAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupCheckmarkBanner() {
checkmarkBannerImageView = UIImageView(image: #imageLiteral(resourceName: "check").withRenderingMode(.alwaysTemplate))
checkmarkBannerImageView?.contentMode = .center
checkmarkBannerImageView?.contentScaleFactor = 4.0
checkmarkBannerImageView?.tintColor = .white
checkmarkBannerImageView?.setBottomBorder(for: .white)
checkmarkBannerImageView?.convertToCircle(value: 12)
photoView.addSubview(checkmarkBannerImageView!)
checkmarkBannerImageView?.anchor(top: photoView.topAnchor, left: nil, bottom: nil, right: photoView.trailingAnchor, paddingTop: 4, paddingLeft: 0, paddingBottom: 0, paddingRight: 4, width: 24, height: 24)
}
func removeCheckmarkBanner() {
checkmarkBannerImageView?.removeFromSuperview()
checkmarkBannerImageView?.constraints.forEach{$0.isActive = false }
checkmarkBannerImageView = nil
}
}
【问题讨论】: