斯威夫特 - 中的UIButton没有UICollectionViewCell点击(Swift -

2019-09-26 03:46发布

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

Answer 1:

为什么键盘敲击单元格时出现的原因是最有可能的,你是在事实上轻敲UITextView ,以防止这种行为只是设置isUserInteractionEnabled标志为假的newsTextView对象。



Answer 2:

更改您的代码:

    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()
    }


}


Answer 3:

imageTapped有PARAM,所以你必须addtarget:

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


文章来源: Swift - UIButton in UICollectionViewCell not clickable