gpt4 book ai didi

swift - UICollectionView didSelectItemAt() 并根据状态添加/删除 View

转载 作者:行者123 更新时间:2023-11-30 11:38:30 29 4
gpt4 key购买 nike

我想向选定的单元格添加复选标记。如果随后取消选择该单元格(因此选择了另一个单元格),我想删除复选标记。顶部 ImageView 独立于 Collection View ,代表用户相机胶卷中选定的单元格。 Collection View 的数据源是从 PHAsset 获取的图像列表。

问题是,如果我不在索引路径处重新加载项目,则取消选择的单元格复选标记不会被删除。另外,如果我滚动浏览 Collection View ,我会看到我什至没有点击的单元格中的随机复选标记。我假设这与细胞的重复使用有关。有人可以帮我吗?

下面是我的 View Controller 和单元格的代码片段...

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
}
}

Selecting a Dish

最佳答案

为了避免重复使用的单元格中出现不需要的内容,您可以重写prepareForReuse()方法,并在其中完成所有设置工作(例如隐藏复选标记)

关于swift - UICollectionView didSelectItemAt() 并根据状态添加/删除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472776/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com