- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个带有 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/
堆栈:Mac OS 10.15.6,Xcode 版本 12.0 (12A7209) 我有一个自行调整大小的收集单元。我覆盖 preferredLayoutAttributesFitting获得自我调整
我有一个 Storyboard,它是在 Xcode 6 中创建的,带有 Collection View ,并且单元格具有不同的大小类变化,例如不同的字体大小......( Storyboard中创建的
我有一个 UICollectionView,只有 1 个部分,该部分中有许多单元格。我启用了拖放行为(使用长按手势识别器,因为 UICollectionView 位于通用 UIViewControll
按照我的老问题here : How to add UIView inside a UITableViewCell Programmatically? 我想通过在这些 collectionViewCel
我将 UICollectionViewCell 与父 UICollectionViewCell 中的按钮一起使用: protocol DayCellDelegate { func button
例如,我有 10 个单元格的 UICollectionView。我想为选定的单元格添加边框,稍后,在选择另一个单元格时,想要删除以前的边框并为新的选定单元格添加边框。 我怎样才能做到这一点? 我已经试
我有一个 UICollectionViewCell(比如 UICollectionViewCell 1),单元格内有一个按钮和 UITextfields。连接到 (ViewController 1)
我在单个 UICollectionViewCell 内有 (3) UITextViews、(1) UITextField 和 (1) UILabel >。当文本换行到第二行时,在符合 UITextVi
我对 TableVieCell 中的 collectionView 有疑问。当我点击 collectionCell 时,didSelectItemAt 不会被调用。我在 collectionCell
我最近在我的 Storyboard中添加了一个 UICollectionView,它当前被另一个 View 插入 View ,这似乎工作正常但是,使用 Storyboard编辑器我将 View 设置为
我想在 UICollectionViewCell 上开始一些动画当用户点击一个单元格时。我的想法是在 didSelectItemAtIndexPath 中选择相应的单元格并触发动画。但是,这不起作用:
我可以通过使用手势识别器拖动它并简化新的 iOS 9 对重新排序的支持来重新排序 iOS 9 上的 UICollectionViewCells。 public func beginInteractiv
我正在开发一个应用程序,其中我在 UICollectionView 中实现了编辑模式。我正在尝试实现如下所示的内容: 当我单击编辑按钮时,相机按钮和眼睛按钮应隐藏,删除和重命名按钮应如下所示: 编辑后
我希望所有单元格之间以及屏幕边框和最近的单元格之间的间距相等。基本上是这样的。内部和周围的间距相等。 我设法通过设置这样的值在界面生成器中测试这一点。我将单元格大小设置为 110,最小间距设置为 0,
我已经设置了一个 UICollectionView,如下所示。我正在尝试设置自定义单元格高度,以便该集合在必须滚动之前适合屏幕上的 3 个单元格。但是,这种定制工作不起作用或似乎被忽略了。有谁知道我做
我在不使用 Storyboard的情况下以编程方式创建了 UICollectionView 和 UICollectionViewCell,但是发生了一些事情,我无法理解为什么。 BKPhotoCell
我正在寻找制作图片库。我希望我的图像全屏显示,但不确定这如何与自动布局一起使用。我知道有一种方法可以返回单元格的大小,但不知道我是否应该只获取屏幕大小并返回它,或者是否有使用自动布局的方法。 最佳答案
我有一个问题, 我有一个简单的 UICollectionView,其中包含从 Flickr 加载图像的静态 200 个单元格。 我的 CellForItemAtIndexPath 看起来像这样: -
以下代码是一个非常简单的程序,用于在 UICollectionView 中显示 1-10000 之间的数字。 .它在不滚动的情况下正确显示,但是如果您向下滚动并向后滚动 Collection View
这个问题在这里已经有了答案: UICollectionViewLayout Not working with UIImage in Swift 5 and Xcode 11 (2 个答案) 关闭 2
我是一名优秀的程序员,十分优秀!