Swift - UICollectionViewCell 中的 UIButton 不可点击 [英] Swift - UIButton in UICollectionViewCell not clickable
问题描述
我目前有一个 UICollectionViewController 来显示我的所有新闻文章.对于我的新闻项目,我创建了一个自定义 UICollectionViewCell,右上角有一个小按钮.
I currently have a UICollectionViewController to display all my news articles. For my news items, I have created a custom UICollectionViewCell with a small button in the top right corner.
问题是它只有在我将 feedCell 设置为 isUserInteractionEnabled = true 时才有效.我只希望 newsMenu 按钮可以点击,而不是整个单元格都可以选择.
The problem is that it is only working when I set my feedCell to isUserInteractionEnabled = true. I only want the newsMenu button to be clickable, not have the whole cell to be selectable.
我如何实现这一目标?
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
,以防止此行为只是将 newsTextView
对象上的 isUserInteractionEnabled
标志设置为 false.
The reason why the keyboard appears when tapping the cell is most likely that you're in fact tapping on the UITextView
, to prevent this behavior just set the isUserInteractionEnabled
flag to false on the newsTextView
object.
这篇关于Swift - UICollectionViewCell 中的 UIButton 不可点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!