ios - UIButton 上的 AddTarget 未触发
问题描述
我有一个UICollectionViewDataSource
定义如下。
class MyDataSource : NSObject, UICollectionViewDataSource {
var tables : [Table]?
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell: TableCVCell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as? TableCVCell {
let row = indexPath.row
let table = tables![row]
cell.isUserInteractionEnabled = true
if table.type == "book" {
cell.actionButton.table = table
cell.actionButton.addTarget(self, action: #selector(btnClicked(sender:)), for: .touchUpInside)
}
return cell
}
return UICollectionViewCell()
}
@objc func btnClicked(sender : ActionButton) {
print("clicked")
}
}
我正在尝试为我的 分配一个目标actionButton
,但是当我单击它时没有任何反应。我确保isUserInteractionEnabled
为单元格和actionButton
.
编辑:
MyDataSource
UIView
在我的定义中使用这里:
class MyView: UIView {
var collectionView : UICollectionView!
let flowLayout = UICollectionViewFlowLayout()
let cvDelegate = MYDelegate()
let dataSource = MyDataSource()
override init(frame: CGRect) {
super.init(frame: frame)
setupCollectionView()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupCollectionView()
}
func setupCollectionView() {
flowLayout.scrollDirection = .vertical
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight), collectionViewLayout: flowLayout)
collectionView.delegate = cvDelegate
collectionView.dataSource = dataSource
collectionView.isUserInteractionEnabled = false
collectionView.register(TableCVCell.self, forCellWithReuseIdentifier: "cell")
}
最后:
MyViewController : UIViewController {
override func loadView() {
let view = MyView()
self.view = view
}
}
解决方案
这里isUserInteractionEnabled
collectionView.delegate = cvDelegate
collectionView.dataSource = dataSource
collectionView.isUserInteractionEnabled = false
默认情况下应该是true
或删除它true
collectionView.isUserInteractionEnabled = true
推荐阅读
- java - 正则表达式获取Java中两个字符之间的文本
- php - 分页自定义博客脚本
- c# - C# TcpClient 和 NetworkStream 通过 TCP 发送/接收数据。为什么不使用“使用”块,我可以在将数据写入流后立即读取数据吗?
- sql - 我如何过滤掉我的数据集上的重复条目,总和为零
- powershell - Sharepoint 在线复制文件脚本
- jquery - 为什么 IE 11 报告链接选择器的“无效参数”:tabbable:first?
- javascript - 如何从包含括号的字符串中访问对象属性
- symfony - 如何在 Symfony 中结合 Twig 使用 Select2 Ajax
- out-of-memory - 带有高度顶点的 JanusGraph OOM 异常
- java - ElasticSearch:Java 高级 API - 具有动态 ID 的索引文档