2

I am trying to change some attributes of the custom view cell in my collection view outside of the cell, but I can not figure it out what I am doing wrong:

In cellForItem I have :

          func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PatientProfileCell", for: indexPath) as! PatientQuestionCell

                if Auth.auth().currentUser?.uid == userID {
                  cell.commentButton.setTitle("Edit post", for: .normal)
                  cell.commentButton.addTarget(self, action: #selector(editPostAction), for: .touchUpInside)
                }
            } else {
                print("Not the current user for collection view")
            }
          }

Everything works perfectly here but when I do trigger the action nothing happens. This is the function :

  //post actions
    func editPostAction()  {
        print("Edit post Enabled")

        let cell = PatientQuestionCell()

        cell.questionView.isEditable = true
        cell.questionView.isSelectable = true
        cell.questionView.isScrollEnabled = true
        cell.questionView.becomeFirstResponder()

    }

How can I change the attributes of the cell with this function?

Marian Petrisor
  • 262
  • 2
  • 19
  • Does the function print "Edit post Enabled" or not? – Mo Abdul-Hameed Jun 24 '17 at 14:31
  • Yes it does! the other part with the cell dosen't work – Marian Petrisor Jun 24 '17 at 14:33
  • Your goal is to change those properties in the cell that you clicked its button, right? – Mo Abdul-Hameed Jun 24 '17 at 14:35
  • Yes! and I am not doing indexPath isn't it? I am trying to achieve something unreachable :) – Marian Petrisor Jun 24 '17 at 14:38
  • You are allocating a new cell instance and modifying that, rather than the cell that contains the button. See [this](https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510) it is about tableviews but the method will work for collection views as well. Be aware that due to cell reuse, your current code will add the action handler to multiple cells multiple times – Paulw11 Jun 24 '17 at 14:38

1 Answers1

5

You can make use of the tag property of UIButton like this:

Inside your collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) method, add this line of code:

cell.commentButton.tag = (indexPath.section * 100) + indexPath.item

Then set the target of your button like this:

cell.commentButton.addTarget(self, action: #selector(editPostAction(sender:)), for: .touchUpInside)

Now, add a new method, which is your selector:

func editPostAction(sender: UIButton) {
    let section = sender.tag / 100
    let item = sender.tag % 100
    let indexPath = IndexPath(item: item, section: section)

    let cell = self.collectionView?.cellForItem(at: indexPath) as! PatientQuestionCell
    cell.questionView.isEditable = true
    cell.questionView.isSelectable = true
    cell.questionView.isScrollEnabled = true
    cell.questionView.becomeFirstResponder()
}
Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36
  • Thank you, this works perfectly! Can you tell me what is 100 in the function so I can understand better what it does ? – Marian Petrisor Jun 24 '17 at 17:33
  • You're welcome. This is only a way to know the cell that button belongs to. For example, the 5th cell in the 2nd section will have a tag equals to: (2 * 100) + 5 = 205. Now: section = 205 / 100 = 2 (Integer division). And item = 205 % 100 = 5. – Mo Abdul-Hameed Jun 24 '17 at 18:28
  • This answer was inspired by a similar issue related to table views that i found an answer for here on Stackoverflow, but i can't find a link to it. – Mo Abdul-Hameed Jun 24 '17 at 18:30
  • That si really smart! Thank you for that. – Marian Petrisor Jun 24 '17 at 20:58
  • OF TOPIC: I was wondering if you know something about this, it is somehow related to same subject : https://stackoverflow.com/questions/44740910/how-to-get-the-bottom-constraint-of-a-cell-to-use-with-keyboard-notification – Marian Petrisor Jun 24 '17 at 21:04
  • You're welcome! I'll take a look at the other question – Mo Abdul-Hameed Jun 24 '17 at 22:29