gpt4 book ai didi

Swift - UICollectionViewCell 中的 UIButton 不可点击

转载 作者:行者123 更新时间:2023-11-28 06:24:05 25 4
gpt4 key购买 nike

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

最佳答案

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

关于Swift - UICollectionViewCell 中的 UIButton 不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42504935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com