Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. //This is the Cell Class
  2.  
  3. protocol CommentFeedProtocol {
  4. func editCommentInFeed(cell: CommentCell, comment: Comment)
  5. }
  6.  
  7. class CommentCell: BaseCell {
  8.  
  9. var delegate: CommentFeedProtocol?
  10.  
  11. let editButton: UIButton = {
  12.  
  13. let editbttn = UIButton(type: UIButton.ButtonType.system)
  14. editbttn.setImage(UIImage(named: "editButton"), for: UIControl.State.normal)
  15. editbttn.addTarget(self, action: #selector(editCommentField), for: UIControl.Event.touchUpInside)
  16.  
  17. return editbttn
  18. }()
  19.  
  20.  
  21. override func setUpCell() {
  22. super.setUpCell()
  23.  
  24. backgroundColor = UIColor.white
  25.  
  26.  
  27. setUpCommentCell()
  28.  
  29. }
  30.  
  31. fileprivate func setUpCommentCell(){
  32.  
  33. addSubview(profileImageView)
  34. addSubview(editButton)
  35. addSubview(commentTextView)
  36. addSubview(seperator)
  37.  
  38. profileImageView.anchor(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, paddingTop: 8, paddingLeft: 8, paddingBottom: 0, paddingRight: 0, width: 40, height: 40)
  39. profileImageView.layer.cornerRadius = 40/2
  40.  
  41. editButton.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 5, paddingLeft: 0, paddingBottom: 5, paddingRight: 10, width: 45, height: 0)
  42.  
  43. commentTextView.anchor(top: topAnchor, left: profileImageView.rightAnchor, bottom: bottomAnchor, right: editButton.leftAnchor, paddingTop: 4, paddingLeft: 4, paddingBottom: 4, paddingRight: 4, width: 0, height: 0)
  44.  
  45. seperator.anchor(top: bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, paddingTop: 5, paddingLeft: 20, paddingBottom: 0, paddingRight: 20, width: 0, height: 1.5)
  46.  
  47.  
  48. }
  49.  
  50.  
  51. /*Objecthandler*/
  52.  
  53. @objc func editCommentField(){
  54. guard let comment = comment else { return}
  55. delegate?.editCommentInFeed(cell: self, comment: comment)
  56. }
  57.  
  58. // This is the CollectionViewController
  59. // As you can see I am also add the Protocol to the class
  60.  
  61. class CommentsController: UICollectionViewController, UICollectionViewDelegateFlowLayout, CommentFeedProtocol {
  62.  
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65.  
  66. navigationItem.title = "Kommentare"
  67.  
  68. postButton.imageView?.alpha = 0.5
  69. postButton.isEnabled = false
  70.  
  71.  
  72. collectionView.keyboardDismissMode = .interactive
  73. collectionView.register(CommentCell.self, forCellWithReuseIdentifier: commentCell)
  74. collectionView.alwaysBounceVertical = true
  75.  
  76. self.collectionView.backgroundColor = UIColor.white
  77.  
  78. fetchComments()
  79.  
  80. }
  81.  
  82.  
  83. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  84. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: commentCell, for: indexPath) as! CommentCell
  85.  
  86.  
  87. cell.comment = comments[indexPath.item]
  88. cell.delegate = self
  89.  
  90. return cell
  91. }
  92.  
  93. // Here is the Delegate Function
  94.  
  95. func editCommentInFeed(cell: CommentCell, comment: Comment) {
  96. print("Button was Pressed")
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement