UIButton上的AddTarget不触发

时间:2019-04-13 18:42:24

标签: ios swift uicollectionview uibutton uicollectionviewcell

我有一个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
    }
}

1 个答案:

答案 0 :(得分:1)

这里for (const call of calls) { if (typeof call[0] !== 'string' || typeof call[1] !== 'string') { throw new Error('Both arguments to myFunction must be strings'); } a.myFunction.apply(undefined, call) }

Get-NetTCPConnection -LocalPort 50000 | Format-List

应为taskkill /F /PID pid或将其删除,默认情况下为isUserInteractionEnabled

collectionView.delegate = cvDelegate
collectionView.dataSource = dataSource 
collectionView.isUserInteractionEnabled = false