- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想向选定的单元格添加复选标记。如果随后取消选择该单元格(因此选择了另一个单元格),我想删除复选标记。顶部 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
}
}
最佳答案
为了避免重复使用的单元格中出现不需要的内容,您可以重写prepareForReuse()方法,并在其中完成所有设置工作(例如隐藏复选标记)
关于swift - UICollectionView didSelectItemAt() 并根据状态添加/删除 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472776/
第一个viewController import UIKit class photosCollectionViewController: UIViewController,UICollect
我的 Collection View 已准备就绪,我正在尝试执行 didSelectItemAt 以转到详细 View 。但我只想测试记录每个项目,但它没有记录。 我已经设置了所有代表: * clas
enter image description here 我在从 searchBar 过滤的 tableView 单元格实现 didSelectRowAtIndexPath 时遇到问题。当我根据搜索栏
在我的 VC 中,我有一个从底部拉起的 View 。我在 viewDidLoad() 中设置并添加了一个 UICollectionView: //Add and setup the collectio
我有一个 UICollection View 。这些东西可以有 3 种状态: - 主动 - 中性 - 无效 现在,这是 UICollectionViewCell 的代码: class NGSelect
我试图在 didSelectItemAt 的帮助下更改 collectionviewcell 中的 UIImage。当我在开始时选择单元格时它工作正常,但是当我向下滚动并向上滚动时,图像恢复到原来的状
我很困惑,因为这只是一个简单的事情,但不起作用。 这是我在这里写问题之前的检查 list 。 UICollectionViewDelegate 已注册 上面除了来自“Debug View Hierar
我在 UICollectionViewCell 中使用 didSelectItemAt 时遇到问题。我正在使用第三方框架从库中选择多张照片。然后,所选 Assets 将被传输并存储在 PHAssets
尝试在 Collection View 中选择单元格时,没有任何反应。我设置了委托(delegate),但当用户点击时它不会被调用。 func collectionView(_ collectionV
我有一个自定义的 UICollectionViewCell,上面放置了一个 UITextField。 UICollectionView 中有多个项目和多种类型的单元格,因此,我只想一次只允许一个文本字
我使用的是 UICollectionView,当用户点击一个单元格时,一个特定的进程就会运行。由于此过程可能需要几秒钟,因此我想在此过程即将开始时立即向用户显示一个带有一些处理消息的 View 。 大
我已经创建了一个包含 GSMMap 和 collectionView(Horizontal) 的 viewController,就像 google map 一样。我使用下面的代码使项目居中并对齐。
我正在尝试制作一个 segue,将字符串发送到下一个 View Controller 中的变量。此字符串是 Collection View 单元格内标签内的文本,标签内的文本来自 CoreData。当
我在 Collection View Cell 上使用单击来调用 collectionView didSelectItemAt 内的 performSegue。有时特别是当应用程序首次启动时,perf
我已经通过 google 和 stack overflow 进行了查看,但找不到答案。我有一个 UICollectionView 并希望在单击单元格时将用户带到另一个 View 。但在这样做之前,我想
我正在使用委托(delegate)在 UICollectionViewCell 和 UICollectionViewController 之间建立连接。在这方面我想说的是,如果用户点击一个 UIVie
我需要实现一个看起来像附加 gif 的动画。 当您点击一个单元格时,它应该放大,因此单元格之间的间距应该保持不变(单元格不应该重叠)。当您选择另一个单元格时,当前选定的单元格动画到原始大小,新选择的单
我在名为 MyCropView 的 UIView 类中的 collectionView 中有一个传感器列表,我在其中显示了有关该裁剪的一些数据。我试图更改某些传感器的值,例如每次单击时的图标和背景颜色
这个问题在这里已经有了答案: Table view didSelectRowAtIndexPath, only works after selecting second click (1 个回答)
我正在使用 SCLAlertView 创建自定义警报 View 。我的警报 View 包含一个文本字段和彩色单元格的 Collection View 问题是 UICollectionView 的 di
我是一名优秀的程序员,十分优秀!