UICollectionView didSelectItemAt 方法不起作用
Posted
技术标签:
【中文标题】UICollectionView didSelectItemAt 方法不起作用【英文标题】:UICollectionView didSelectItemAt method doesn't work 【发布时间】:2021-05-15 11:54:01 【问题描述】:当我在 UICollectionView 中点击项目时需要给出一些逻辑,但我的 didSelectItemAt 方法不起作用
这是我的 ViewController 代码
class NewGalleryViewController: UIViewController
var presenter: ViewToPresenterPhotoProtocol?
var builder: GalleryRequestBuilder?
@IBOutlet var collectionView: UICollectionView!
let refreshControl = UIRefreshControl()
let reuseIdentifier = "customCVCell"
@objc func refresh()
presenter?.refresh()
override func viewDidLoad()
super.viewDidLoad()
self.collectionView.refreshControl = refreshControl
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
self.setupPresenterIfNeed()
presenter?.viewDidLoad()
// Do any additional setup after loading the view.
func setupPresenterIfNeed()
self.collectionView.backgroundColor = UIColor.white
if self.presenter == nil
let presenter = GalleryPresenter()
presenter.view = self
self.presenter = presenter
self.builder = GalleryRequestBuilder()
extension NewGalleryViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
return self.presenter?.photos.count ?? 0
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PhotoCollectionViewCell
cell.cellSetup(photo: (self.presenter?.photos[indexPath.item])!)
if indexPath.row == (self.presenter?.photos.count)! - 1
self.presenter?.fetchNewPhotos()
return cell
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize
return CGSize(width: 180, height: 128)
private func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
// handle tap events
print("test Tap")
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumInteritemSpacingForSectionAt section: Int) -> CGFloat
return 1.0
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat
return 20.0
extension NewGalleryViewController: PresenterToViewPhotoProtocol
func onFetchPhotoSuccess()
self.collectionView.reloadData()
self.collectionView!.collectionViewLayout.invalidateLayout()
self.collectionView!.layoutSubviews()
self.refreshControl.endRefreshing()
func onFetchPhotoFailure(error: String)
print("View receives the response from Presenter with error: \(error)")
self.collectionView.refreshControl?.endRefreshing()
这是我的 CollectioViewCell 代码
class PhotoCollectionViewCell: UICollectionViewCell
var builder = GalleryRequestBuilder()
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib()
super.awakeFromNib()
self.imageView.isUserInteractionEnabled = false
// Initialization code
@objc func navigateToDetailView()
func cellSetup(photo: Photo)
self.imageView.kf.setImage(with: builder.createImageUrl(name: photo.image.name))
self.imageView.layer.cornerRadius = 8
self.contentView.layer.cornerRadius = 8
self.layer.borderColor = UIColor.opaqueSeparator.cgColor
self.layer.borderWidth = 1
self.layer.shadowColor = UIColor.black.cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
self.layer.shadowOpacity = 0.3
self.layer.shadowRadius = 0.0
self.layer.cornerRadius = 8.0
self.layer.masksToBounds = false
我认为问题可能出在 ImageView 但删除后 didSelectItemAt 方法仍然无法正常工作。
如果您需要更多信息,这里是 GitHub 存储库https://github.com/Feetelcore/SwiftWebantGallery
【问题讨论】:
【参考方案1】:问题是您将 didSelectItemAtIndexPath 函数定义为私有。当您这样做时,它不代表 UICollectionView 的委托功能。您应该从函数中删除私有并定义如下。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
print("siski")
【讨论】:
我收到警告 实例方法 'collectionView(collectionView:didSelectItemAtIndexPath:)' 几乎符合协议 'UICollectionViewDelegate' 的可选要求 'collectionView(_:didSelectItemAt:)' Make 'collectionView(collectionView:didSelectItemAtIndexPath: )' private 以使此警告静音 因为你写错了它说几乎匹配不完全匹配可能你正在做错字错误。所以只需编写 didSelect 并让 xCode 自动完成它。我下载你的回购并做同样的工作。 @伊万纽斯【参考方案2】:我发现您缺少delegate
。在viewDidload
中添加这一行,然后重试:
collectionView.delegate = self
【讨论】:
您将private
用于didSelectItemAtIndexPath
。请删除private
并再次通过建议符合委托。以上是关于UICollectionView didSelectItemAt 方法不起作用的主要内容,如果未能解决你的问题,请参考以下文章
使用 didSelectRowAtIndexPath 进行转场
Swift UITableView didSelectRowAtIndexPath 错误
-[UICollectionView _endItemAnimations] 中的 UICollectionView 断言失败