AddTarget на UIButton не запускается - PullRequest
0 голосов
/ 13 апреля 2019

У меня есть 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 Ответ

1 голос
/ 13 апреля 2019

Здесь isUserInteractionEnabled

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

Должно быть true или удалить его, так как по умолчанию это true

collectionView.isUserInteractionEnabled = true
...