[英]AddTarget on UIButton not firing
I have a UICollectionViewDataSource
defined as follows. 我有一个
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")
}
}
I'm trying to assign a target for my actionButton
, however nothing happens when I click it. 我正在尝试为我的
actionButton
分配一个目标,但是当我单击它时什么也没有发生。 I made sure to enable isUserInteractionEnabled
for the cell and the actionButton
. 我确保为单元格和
actionButton
启用isUserInteractionEnabled
。
EDIT: 编辑:
MyDataSource
is used here in my UIView
definition: 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")
}
and finally: 最后:
MyViewController : UIViewController {
override func loadView() {
let view = MyView()
self.view = view
}
}
Here isUserInteractionEnabled
这是
isUserInteractionEnabled
collectionView.delegate = cvDelegate
collectionView.dataSource = dataSource
collectionView.isUserInteractionEnabled = false
Should be true
or remove it as by default it's true
应该为
true
或将其删除,因为默认情况下为true
collectionView.isUserInteractionEnabled = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.