如何从 Swift 中的不同 class 访问 collectionView

How to access collectionView from a different class in Swift

我子class编辑了 CollectionViewCell,我添加了不同的标签、动画和按钮。

我正在尝试在此 class(CollectionViewCell) 中的一个方法中删除一个单元格并更新 collectionView,但我无法从此处访问 collectionView,因为它是在另一个 [=26] 中声明的=].

我的收藏视图Class:

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

@IBOutlet weak var collectionView: UICollectionView!
}

我的 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()//

  }

就像@rdelmar 说的那样,最好将 panGestureRecognizer 放入控制器中。

然后您可以使用 collectionView.indexPathForItemAtPoint(recognizer.locationInView(collectionView))

访问 indexPath

否则,您可以使用 NSNotificationCenter:

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

并在控制器中

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

然后执行:

func cellPanned(notification:NSNotification) {
    let gestureRecognizer = notification.object as! UIPanGestureRecognizer
    // Handle pan
}