- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尽管可能是主观的,但我想知道如何进行自定义 UICollectionViewCell
那当它的UIButton
按下,通知自定义 UICollectionViewController
做什么。
我的第一个想法是使用 delegate
在 CustomCell
如下:
class CustomCell: UICollectionViewCell {
var delegate: CustomCellDelegate?
static let reuseIdentifier = "CustomCell"
@IBOutlet weak private var button: UIButton! {
didSet {
button.addTarget(self, action: #selector(self.toggleButton), for: .touchUpInside)
}
}
@objc private func toggleButton() {
delegate?.didToggleButton()
}
}
CustomCellDelegate
的类协议(protocol)在哪里定义为:
protocol CustomCellDelegate: class {
func didToggleButton()
}
UICollectionViewController
然后实现
didToggleButton
函数并将自身指定为
delegate
每个单元格如下:
class CustomCollectionViewController: UICollectionViewController, CustomCellDelegate {
func didToggleButton() {
// do some stuff and then update the cells accordingly ...
collectionView?.reloadData()
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let customCell = collectionView.dequeueReusableCell(withReuseIdentifier: CustomCell.reuseIdentifier, for: indexPath) as? CustomCell else { fatalError("Unexpected indexPath") }
customCell.delegate = self
return customCell
}
}
UICollectionViewCell
之间进行通信?及其父 Controller ?
最佳答案
是的,这肯定是正确的解决方案。 您的自定义单元是盲目的,它们对您的 Controller 一无所知。他们只触发委托(delegate)方法。
但是,还有另一种正确的解决方案,那就是观察。 有人喜欢委托(delegate),有人喜欢观察。您可以使用 NotificationCenter
发布有关在您的单元格中发生的触摸的通知,并使您的 Controller 成为对这些通知作出 react 的观察者。
// inside your cell
NotificationCenter.default.post(name: Notification.Name("ButtonPressed"), object: nil)
// inside your controller
NotificationCenter.default.addObserver(self, selector: #selector(someHandler), name: Notification.Name("ButtonPressed"), object: nil)
someHandler()
当您的 Controller (观察者)捕获已发布的事件时,将处理该调用。
// in your controller
cell.pressHandler = {
// do something
...
}
// in your cell
var pressHandler: (() -> Void)?
...
// when the button is pressed you execute that handler
pressHandler?()
关于ios - UICollectionViewCell 与它的 UICollectionViewController 对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50039663/
我正在编写一个项目,我需要在水平 Collection View Controller /列表中显示玩家(头像)列表,我们称其为 HUD。 我希望这个 HUD 是通用的,也就是说:我知道这个组件会在整
我已经实现了自定义 UITabBarController 第一个和第二个 UITabBarItem 是一个 UICollectionViewController 第四个 UITabBarItem 是一
我想在我的应用中使用 UICollectionViewController 来显示照片。 我从 UICollectionViewController 派生了一个类: #import @interfa
我可以使用 self.navigationItem.leftBarButtonItem = self.editButtonItem; 通过按下导航面板上的编辑按钮让 UITableViewContro
我想在 iOS 6 下的 UICollectionViewController 中实现下拉刷新。使用 UITableViewController 可以轻松实现这一点,如下所示: UIRefreshCo
我收到错误: fatal error: unexpectedly found nil while unwrapping an Optional value didTapUserScreenButton
我在从子 UICollectionViewController 属性访问父属性时遇到问题。 父级是模态的 ChildViewController,它添加 UICollectionViewControl
当我使用以下代码在 UIViewController 中的 TableView 中选择一个单元格时,我一直试图连接到 UICollectionViewController: func tableVie
我的 collectionViewController 及其数据加载有问题... 我正在尝试加载一个外部 JSON 文件... 第一个问题是当应用程序加载时,CollectionViewControl
我正在尝试在 tabBarController 上添加 CollectionView 对于网格集合View 所以我制作了一个 UICollectionViewController 作为另一个 swif
我正在尝试布局 View 网格。每行应该有 3 个 View 。我试图定义我的 UICollectionViewController的构造函数如下: - (id)initWithDefaultLayo
我刚刚以编程方式创建了一个UICollectionViewController。到目前为止,一切都很好,但我面临一些问题。当调用 CollectionViewController 时,我的应用程序立即
我有一个 UICollectionViewController,它就像一个分页,每个单元格都有自己的页面。在导航栏上,它始终显示“返回”。以下不会更改标题。 -(void)viewWillAppear
这个问题在这里已经有了答案: Swift: IBoutlets are nil in Custom Cell (2 个答案) 关闭 5 年前。 Xcode 9.2、 swift 4我的 Main.s
我有一个名为 ProfileCollectionViewController 的 Collection View ,用于收集图像。单击图像时,它会显示一个全屏显示图像的 HorizontalPro
我有一个简单的 UICollectionViewController,它是一个带有固定到可见边界的页眉和页脚的部分。当我单击页脚中的 textField 时,页脚会自动在键盘上方设置动画,并且 col
我可以使用以下代码从 appdelegate 启动 FriendsController(UICollectionViewController): window = UIWindow(frame: UI
使用 UICollectionViewController 我可以轻松启用重新排序。但要真正重新排序,我必须长按单元格才能开始重新排序状态。 (注意从圆圈到单元格实际移动的延迟。) 由于我以类似跳板的
我正在使用 ParseFramework 获取一些图像并将它们显示在 collectionViewController 中。问题是,如果我在加载所有图像之前滚动,我会遇到图像重复,所以我会在不同的单元
我试图显示我的收藏 View ,但我得到的只是黑屏。我知道我在某处遗漏了一些东西,但我似乎找不到它是什么。 #import "StoreCollectionViewController.h" #imp
我是一名优秀的程序员,十分优秀!