I have a Protocol inside my CellClass which is detect the function 'editCommentInFeed'. I have create an 'editButton' inside my Cell and define the handler with 'editCommentField' which is calling per Delegate the function and over give the 2 Parameters. This 2 Parameters are important I need this Informations from the cell to detect in future actions which will be provided.
For example I had tried to print something when the Button ist Pressed inside the Cell. But even this Statement is not execute.
//This is the Cell Class
protocol CommentFeedProtocol {
func editCommentInFeed(cell: CommentCell, comment: Comment)
}
class CommentCell: BaseCell {
var delegate: CommentFeedProtocol?
let editButton: UIButton = {
let editbttn = UIButton(type: UIButton.ButtonType.system)
editbttn.setImage(UIImage(named: "editButton"), for: UIControl.State.normal)
editbttn.addTarget(self, action: #selector(editCommentField), for: UIControl.Event.touchUpInside)
return editbttn
}()
override func setUpCell() {
super.setUpCell()
backgroundColor = UIColor.white
setUpCommentCell()
}
fileprivate func setUpCommentCell(){
addSubview(profileImageView)
addSubview(editButton)
addSubview(commentTextView)
addSubview(seperator)
profileImageView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40)
profileImageView.layer.cornerRadius = 40/2
editButton.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 5, paddingLeft: 0, paddingBottom: 5, paddingRight: 10, width: 45, height: 0)
commentTextView.anchor(top: topAnchor, left: profileImageView.rightAnchor, bottom: bottomAnchor, right: editButton.leftAnchor, paddingTop: 4, paddingLeft: 4, paddingBottom: 4, paddingRight: 4, width: 0, height: 0)
seperator.anchor(top: bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 5, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 1.5)
}
/*Objecthandler*/
@objc func editCommentField(){
guard let comment = comment else { return}
delegate?.editCommentInFeed(cell: self, comment: comment)
}
// This is the CollectionViewController
// As you can see I am also add the Protocol to the class
class CommentsController: UICollectionViewController, UICollectionViewDelegateFlowLayout, CommentFeedProtocol {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Kommentare"
postButton.imageView?.alpha = 0.5
postButton.isEnabled = false
collectionView.keyboardDismissMode = .interactive
collectionView.register(CommentCell.self, forCellWithReuseIdentifier: commentCell)
collectionView.alwaysBounceVertical = true
self.collectionView.backgroundColor = UIColor.white
fetchComments()
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: commentCell, for: indexPath) as! CommentCell
cell.comment = comments[indexPath.item]
cell.delegate = self
return cell
}
// Here is the Delegate Function
func editCommentInFeed(cell: CommentCell, comment: Comment) {
print("Button was Pressed")
}
You can see inside the example that I am Adding the Protocol to the class and also call the function inside the Class. I am not getting a Error Message. The Button is only light hovered when is clicked, but don't execute anything ?!