我添加了不同的标签,动画和按钮,从而将CollectionViewCell细分为子类。
我试图删除一个单元格并更新该类中的一个方法(CollectionViewCell)中的collectionView,但是由于它是在另一个类/ viewController中声明的,因此我无法从此处访问collectionView。
我的CollectionView类别:
//this is how I have declared the collectionView.
class FirstViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
@IBOutlet weak var collectionView: UICollectionView!
}
我的CollectionViewCell类:
//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放入控制器中。
然后您可以使用以下命令访问indexPath collectionView.indexPathForItemAtPoint(recognizer.locationInView(collectionView))
否则,您可以将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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句