UICollectionViewCell中的Swift-UIButton不可单击

UICollectionViewCell中的Swift-UIButton不可单击,swift,uicollectionview,Swift,Uicollectionview,我目前有一个UICollectionViewController来显示我的所有新闻文章。对于我的新闻项目,我创建了一个自定义UICollectionViewCell,在右上角有一个小按钮 问题是,只有当我将feedCell设置为isUserInteractionEnabled=true时,它才起作用。我只希望newsMenu按钮是可点击的,而不是整个单元格都可选择 我如何做到这一点 override func collectionView(_ collectionView: UICollecti

我目前有一个UICollectionViewController来显示我的所有新闻文章。对于我的新闻项目,我创建了一个自定义UICollectionViewCell,在右上角有一个小按钮

问题是,只有当我将feedCell设置为isUserInteractionEnabled=true时,它才起作用。我只希望newsMenu按钮是可点击的,而不是整个单元格都可选择

我如何做到这一点

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let feedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell

    feedCell.post = m_Collection?[(indexPath as NSIndexPath).item]

    feedCell.newsMenu.addTarget(self, action: #selector(imageTapped), for: .touchUpInside)

    return feedCell
}

func imageTapped(button: UIButton)
{
    let newsAction = UIAlertController(title: "Message", message: "Do you want to edit or delete this news message?", preferredStyle: .actionSheet)

    let editAction = UIAlertAction(title: "Edit news", style: .default, handler: menuEditNews)
    let deleteAction = UIAlertAction(title: "Delete news", style: .destructive, handler: menuDeleteNews)
    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)

    newsAction.addAction(editAction)
    newsAction.addAction(deleteAction)
    newsAction.addAction(cancelAction)

    self.present(newsAction, animated: true, completion: nil)
}
我的自定义单元格:

class FeedCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)

    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let titleLabel: UILabel = {
    let label = UILabel()
    label.adjustsFontSizeToFitWidth = false
    label.lineBreakMode = .byTruncatingTail
    label.numberOfLines = 2
    return label
}()

let profileImageView: UIImageView = {
    let imageView = UIImageView()
    imageView.translatesAutoresizingMaskIntoConstraints = false
    imageView.image = UIImage(named: "")
    imageView.layer.cornerRadius = 22
    imageView.layer.masksToBounds = true
    return imageView
}()

let newsTextView: UITextView = {
    let textView = UITextView()
    textView.isScrollEnabled = false
    textView.font = UIFont (name: "Helvetica", size: 13)
    return textView
}()

let newsMenu: UIButton = {
    let newsMenu = UIButton()
    newsMenu.isUserInteractionEnabled = true
    newsMenu.setImage(UIImage(named: "news_menuitem"), for: UIControlState())
    return newsMenu
}()

func setupViews() {
    backgroundColor = UIColor.white

    addSubview(titleLabel)
    addSubview(profileImageView)
    addSubview(newsTextView)
    addSubview(newsMenu)

    addConstraintsWithFormat("H:|-8-[v0(44)]-8-[v1]-10-[v2(25)]-8-|", views: profileImageView, titleLabel, newsMenu)
    addConstraintsWithFormat("H:|-4-[v0]-4-|", views: newsTextView)
    addConstraintsWithFormat("V:|-6-[v0]", views: newsMenu)
    addConstraintsWithFormat("V:|-10-[v0]", views: titleLabel)
    addConstraintsWithFormat("V:|-8-[v0(44)]-4-[v1]", views: profileImageView, newsTextView)
}
}
将代码更改为:

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let feedCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! FeedCell

        feedCell.post = m_Collection?[(indexPath as NSIndexPath).item]

        feedCell.sourceController = self // assign controller

        return feedCell
    }

    func imageTapped() // remove (button: UIButton)
    {
        let newsAction = UIAlertController(title: "Message", message: "Do you want to edit or delete this news message?", preferredStyle: .actionSheet)

        let editAction = UIAlertAction(title: "Edit news", style: .default, handler: menuEditNews)
        let deleteAction = UIAlertAction(title: "Delete news", style: .destructive, handler: menuDeleteNews)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)

        newsAction.addAction(editAction)
        newsAction.addAction(deleteAction)
        newsAction.addAction(cancelAction)

        self.present(newsAction, animated: true, completion: nil)
    }
以及您的自定义单元格

class FeedCell: UICollectionViewCell {

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    weak var sourceController: YourController?  // declare new variable

    let titleLabel: UILabel = {
        let label = UILabel()
        label.adjustsFontSizeToFitWidth = false
        label.lineBreakMode = .byTruncatingTail
        label.numberOfLines = 2
        return label
    }()

    let profileImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.translatesAutoresizingMaskIntoConstraints = false
        imageView.image = UIImage(named: "")
        imageView.layer.cornerRadius = 22
        imageView.layer.masksToBounds = true
        return imageView
    }()

    let newsTextView: UITextView = {
        let textView = UITextView()
        textView.isScrollEnabled = false
        textView.font = UIFont (name: "Helvetica", size: 13)
        return textView
    }()

    lazy var newsMenu: UIButton = {  // change `let` to `lazy var`
        let newsMenu = UIButton()
        newsMenu.isUserInteractionEnabled = true
        newsMenu.setImage(UIImage(named: "news_menuitem"), for: UIControlState())
        newsMenu.addTarget(self, action: #selector(actionTap), for: .touchUpInside) // add target to button
        return newsMenu
    }()

    func setupViews() {
        backgroundColor = UIColor.white

        addSubview(titleLabel)
        addSubview(profileImageView)
        addSubview(newsTextView)
        addSubview(newsMenu)

        addConstraintsWithFormat("H:|-8-[v0(44)]-8-[v1]-10-[v2(25)]-8-|", views: profileImageView, titleLabel, newsMenu)
        addConstraintsWithFormat("H:|-4-[v0]-4-|", views: newsTextView)
        addConstraintsWithFormat("V:|-6-[v0]", views: newsMenu)
        addConstraintsWithFormat("V:|-10-[v0]", views: titleLabel)
        addConstraintsWithFormat("V:|-8-[v0(44)]-4-[v1]", views: profileImageView, newsTextView)
    }

    func actionTap() {  // handle function
         sourceController?. imageTapped()
    }


}

点击单元格时出现键盘的原因很可能是您实际上正在点击
UITextView
,为了防止这种行为,只需在
newsTextView
对象上将
isUserInteractionEnabled
标记设置为false即可。

imageTapped have param,因此必须添加目标:

feedCell.newsMenu.addTarget(self, action: #selector(imageTapped(_:)), for: .touchUpInside)

你为什么不想让整个单元格都可以打开?它是高亮显示的吗?它当前未高亮显示,但如果我将交互设置为true,它也会自动加载键盘。我能关掉这个吗?@DennisvanMazijk检查我的答案,并让我知道它是否适合你。