gpt4 book ai didi

ios - 无法单击自定义 UICollectionViewCell

转载 作者:搜寻专家 更新时间:2023-11-01 06:03:46 24 4
gpt4 key购买 nike

我有一个带有 imageView 和标签的自定义 UICollectionView 单元格,如下所示:

import UIKit

class PollCell: UICollectionViewCell {


@IBOutlet weak var imageView: UIImageView!

@IBOutlet weak var pollQuestion: UILabel!

}

我想让每个填充的单元格都可以点击,然后将信息从被点击的单元格传递到一个新的 ViewController。下面是填充自定义单元格的 ViewController。

import UIKit
import FirebaseDatabase
import FirebaseDatabaseUI
import FirebaseStorageUI


private let reuseIdentifier = "PollCell"

class TrendingViewController: UICollectionViewController {

var ref: FIRDatabaseReference!
var dataSource: FUICollectionViewDataSource!

override func viewDidLoad() {
super.viewDidLoad()

ref = FIRDatabase.database().reference()


// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Register cell classes

self.dataSource = self.collectionView?.bind(to: self.ref.child("Polls")) { collectionView, indexPath, snap in
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! PollCell
/* populate cell */
cell.pollQuestion.text = snap.childSnapshot(forPath: "question").value as! String?
let urlPollImage = snap.childSnapshot(forPath: "image_URL").value as! String?
cell.imageView.sd_setImage(with: URL(string: urlPollImage!), placeholderImage: UIImage(named: "Fan_Polls_Logo.png"))
//Comment
return cell
}

// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath)
}


}

我还有一些代码可以“反转”填充在我的反转 ViewController 中的单元格:

import Foundation
import UIKit

class InvertedStackLayout: UICollectionViewLayout {
let cellHeight: CGFloat = 100.00 // Your cell height here...

override func prepare() {
super.prepare()
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var layoutAttrs = [UICollectionViewLayoutAttributes]()

if let collectionView = self.collectionView {
for section in 0 ..< collectionView.numberOfSections {
if let numberOfSectionItems = numberOfItemsInSection(section) {
for item in 0 ..< numberOfSectionItems {
let indexPath = IndexPath(item: item, section: section)
let layoutAttr = layoutAttributesForItem(at: indexPath)

if let layoutAttr = layoutAttr, layoutAttr.frame.intersects(rect) {
layoutAttrs.append(layoutAttr)
}
}
}
}
}

return layoutAttrs
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let layoutAttr = UICollectionViewLayoutAttributes(forCellWith: indexPath)
let contentSize = self.collectionViewContentSize

layoutAttr.frame = CGRect(
x: 0, y: contentSize.height - CGFloat(indexPath.item + 1) * cellHeight,
width: contentSize.width, height: cellHeight)

return layoutAttr
}

func numberOfItemsInSection(_ section: Int) -> Int? {
if let collectionView = self.collectionView,
let numSectionItems = collectionView.dataSource?.collectionView(collectionView, numberOfItemsInSection: section)
{
return numSectionItems
}

return 0
}

override var collectionViewContentSize: CGSize {
get {
var height: CGFloat = 0.0
var bounds = CGRect.zero

if let collectionView = self.collectionView {
for section in 0 ..< collectionView.numberOfSections {
if let numItems = numberOfItemsInSection(section) {
height += CGFloat(numItems) * cellHeight
}
}

bounds = collectionView.bounds
}

return CGSize(width: bounds.width, height: max(height, bounds.height))
}
}

override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
if let oldBounds = self.collectionView?.bounds,
oldBounds.width != newBounds.width || oldBounds.height != newBounds.height
{
return true
}

return false
}
}

最佳答案

你必须实现UICollectionViewDelegate的这个方法

   public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

debugPrint("this works")
}

希望对你有帮助

关于ios - 无法单击自定义 UICollectionViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42990218/

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