OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionView 按钮 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:18
标题: ios - UICollectionView 按钮

我正在尝试使用按钮创建 UICollectionView。我可以设置按钮标题,但目标操作不起作用。以下是我的代码:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TVButtonCell", for: indexPath) as! TVButtonCell
    cell.channelButton.setTitle(channelKeys[indexPath.row], for: .normal)
    cell.channelButton.addTarget(self, action: #selector(FirstViewController.pressed(_), for: .primaryActionTriggered)
    return cell
}

    func pressed(_ sender: AnyObject) {
    print("Button pressed")
}

有人知道我做错了什么吗?



Best Answer-推荐答案


该死, 需要添加以下功能:

    func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
    return false
}

关于ios - UICollectionView 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41048523/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4