-1

I subclassed CollectionViewCell were I add different labels, animations and buttons.

I am trying to delete a cell and update the collectionView in one of my methods in this class(CollectionViewCell) but I'm unable to access collectionView from here since it was declared in another class/viewController.

My CollectionView Class:

//this is how I have declared the collectionView.
class FirstViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var collectionView: UICollectionView!
}

My CollectionViewCell Class:

//this is what I'm trying to achieve although I know it's incorrect.
class CollectionViewCell: UICollectionViewCell {

func handlePanGesture(recognizer: UIPanGestureRecognizer) {

 FirstViewController.collectionView.reloadData()//

  }
Victor --------
  • 512
  • 1
  • 11
  • 29
  • It would be better to make the controller the target of the gesture recognizer. The cell class shouldn't know about the collection view, and shouldn't be responsible for deleting a cell -- that's the controller's job. – rdelmar May 29 '15 at 20:54

1 Answers1

1

Like @rdelmar says that's better do put the panGestureRecognizer into the controller.

Then you can access the indexPath with collectionView.indexPathForItemAtPoint(recognizer.locationInView(collectionView))

Otherwise, you can use NSNotificationCenter with:

NSNotificationCenter.defaultCenter().postNotification(name: "CellPanned", object: recognizer)

and in the controller

NSNotificationCenter.defaultCenter().addObserver(self,selector:"cellPanned:" name: "CellPanned")

and then implement:

func cellPanned(notification:NSNotification) {
    let gestureRecognizer = notification.object as! UIPanGestureRecognizer
    // Handle pan
}
flovilmart
  • 1,759
  • 1
  • 11
  • 18
  • hm, I can't put the gesture recognizer in the FirstViewController because the recognizer uses some NSLayoutConstrains declared and hooked up in this class. I'll try the second option and will post back. – Victor -------- May 29 '15 at 21:13
  • Looks like it's working but how would I access the indexPath in func cellPanned(notification:NSNotification) { } ? – Victor -------- May 29 '15 at 21:38
  • The same way as method 1 : collectionView.indexPathForItemAtPoint(recognizer.locationInView(collectionView)) – flovilmart May 30 '15 at 17:47
  • there's no "recognizer" variable in fun cellPanned, I only have the "notification" variable in that method and I have a hard time trying to figure out how to get the location in view from a notification. The recognizer is still in the CollectionViewCell class because as I said earlier, I have to have it there because of other variables and constrains I'm performing on the collectionViewCell.... – Victor -------- May 30 '15 at 19:09
  • When you post the notification with : `NSNotificationCenter.defaultCenter().postNotification(name: "CellPanned", object: recognizer)` upon each call of didPan:, The recognizer would be notification.object – flovilmart May 30 '15 at 21:24
  • And still not matter how many excuses you have for architecturing it that way, all of those are not valid! :) – flovilmart May 30 '15 at 21:25
  • Thanks again for your time! I'm such a noob, didn't see I had the object set to nil...although I still disagree with putting the recognizer in the controller so I just ended up using collectionView.indexPathForItemAtPoint(notification.object!.locationInView(collectionView))! – Victor -------- May 30 '15 at 21:46
  • @Victor-------- I've been using iOS for the past 5 years, like you said, you're a noob. I was doing the same mistakes as you before. You should take the advices from experienced developers :) – flovilmart May 31 '15 at 00:15