0

I wanted to modify the cell UI based the documentation mentioned here at enter link description here, but couldn't figure how to translate the super part.

- (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSQMessagesCollectionViewCell *cell = (JSQMessagesCollectionViewCell *)[super collectionView:collectionView cellForItemAtIndexPath:indexPath];

    return cell;
}

Here is what I have translated:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = super.collectionView.cellForItemAtIndexPath(indexPath)

    return cell!
}
Grace Huang
  • 5,355
  • 5
  • 30
  • 52
  • Do you subclass `JSQMessagesViewController` ? It would be helpful if you could provide more code btw... – the_critic Aug 31 '15 at 23:26
  • Yes I do subclass JSQMessagesViewController. I'm not familiar with Obj C. So the sample code here is a bit hard for me to understand. – Grace Huang Aug 31 '15 at 23:29
  • Is it recognizing the class ? I.e. no compilation/linking problems ? – the_critic Aug 31 '15 at 23:30
  • Yes. I updated my question with my translation. It built successfully, but whenever it goes the view, it crashes with this error `fatal error: unexpectedly found nil while unwrapping an Optional value`. Then I noticed that cell is nil, but this function expects to be not nil. – Grace Huang Aug 31 '15 at 23:37
  • That's because you are calling it on super's collectionView as opposed to calling the super function. Try the answer I posted below. – the_critic Aug 31 '15 at 23:42
  • Thanks @the_critic! I got an error ` Overriding method with selector 'collectionView:cellForItemAtIndexPath:' has incompatible type '(JSQMessagesCollectionView, NSIndexPath) -> UICollectionViewCell'` when I copied your code. Do you know why? – Grace Huang Aug 31 '15 at 23:47
  • You'll need to provide more code... I looked into the repository, I am pretty sure the answer I gave should work fine. The `JSQMessagesViewController` conforms to a protocol that extends the UICollectionViewDataSource protocol, which means the super method should work fine. I made an edit in my answer, try it that way. – the_critic Sep 01 '15 at 00:04
  • Just downloaded the library and it seems to compile for me with the code I have posted... – the_critic Sep 01 '15 at 00:21

1 Answers1

2

If you are looking for the correct syntax, here is the solution:

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
      let cell = super.collectionView(collectionView, cellForItemAtIndexPath: indexPath) as! JSQMessagesCollectionViewCell
      return cell
}
the_critic
  • 12,720
  • 19
  • 67
  • 115