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