UICollectionView 手势事件
UICollectionView gesture event
我想在 UICollectionView
中长按单元格时显示删除按钮。当我单击单元格时它会显示,当单击单元格外部时它会出现异常 "fatal error: unexpectedly found nil while unwrapping an Optional value"
如何解决?我的代码如下
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state == UIGestureRecognizerState.Began
{
let p = gestureReconizer.locationInView( self.sectionImageCell._collectionView!)
let touchedIndexPath : NSIndexPath? = self.sectionImageCell._collectionView!.indexPathForItemAtPoint(p)!//Here getting exception when click on outside the cell in a uicollectionview
if touchedIndexPath != nil {
for item in sectionImageCell._collectionView!.visibleCells() as! [CollectionViewcell] {
let indexpath : NSIndexPath = self.sectionImageCell._collectionView!.indexPathForCell(item as CollectionViewcell)!
let cell : CollectionViewcell = self.sectionImageCell._collectionView!.cellForItemAtIndexPath(indexpath) as! CollectionViewcell
//Close Button
if touchedIndexPath == indexpath {
if cell._closeBtn.hidden == false {
cell._closeBtn.hidden = true
}
else {
cell._closeBtn.hidden = false
}
}
}
}
}
}
用if let
解开self.sectionImageCell._collectionView!.indexPathForItemAtPoint(p)!
,你的问题就解决了。
试试这个:
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state == UIGestureRecognizerState.Began
{
let p = gestureReconizer.locationInView( self.sectionImageCell._collectionView!)
if let touchedIndexPath : NSIndexPath = self.sectionImageCell._collectionView!.indexPathForItemAtPoint(p)! as? NSIndexPath//Here getting exception when click on outside the cell in a uicollectionview
{
for item in sectionImageCell._collectionView!.visibleCells() as! [CollectionViewcell] {
let indexpath : NSIndexPath = self.sectionImageCell._collectionView!.indexPathForCell(item as CollectionViewcell)!
let cell : CollectionViewcell = self.sectionImageCell._collectionView!.cellForItemAtIndexPath(indexpath) as! CollectionViewcell
//Close Button
if touchedIndexPath == indexpath {
if cell._closeBtn.hidden == false {
cell._closeBtn.hidden = true
}
else {
cell._closeBtn.hidden = false
}
}
}
}
}
}
我想在 UICollectionView
中长按单元格时显示删除按钮。当我单击单元格时它会显示,当单击单元格外部时它会出现异常 "fatal error: unexpectedly found nil while unwrapping an Optional value"
如何解决?我的代码如下
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state == UIGestureRecognizerState.Began
{
let p = gestureReconizer.locationInView( self.sectionImageCell._collectionView!)
let touchedIndexPath : NSIndexPath? = self.sectionImageCell._collectionView!.indexPathForItemAtPoint(p)!//Here getting exception when click on outside the cell in a uicollectionview
if touchedIndexPath != nil {
for item in sectionImageCell._collectionView!.visibleCells() as! [CollectionViewcell] {
let indexpath : NSIndexPath = self.sectionImageCell._collectionView!.indexPathForCell(item as CollectionViewcell)!
let cell : CollectionViewcell = self.sectionImageCell._collectionView!.cellForItemAtIndexPath(indexpath) as! CollectionViewcell
//Close Button
if touchedIndexPath == indexpath {
if cell._closeBtn.hidden == false {
cell._closeBtn.hidden = true
}
else {
cell._closeBtn.hidden = false
}
}
}
}
}
}
用if let
解开self.sectionImageCell._collectionView!.indexPathForItemAtPoint(p)!
,你的问题就解决了。
试试这个:
func handleLongPress(gestureReconizer: UILongPressGestureRecognizer) {
if gestureReconizer.state == UIGestureRecognizerState.Began
{
let p = gestureReconizer.locationInView( self.sectionImageCell._collectionView!)
if let touchedIndexPath : NSIndexPath = self.sectionImageCell._collectionView!.indexPathForItemAtPoint(p)! as? NSIndexPath//Here getting exception when click on outside the cell in a uicollectionview
{
for item in sectionImageCell._collectionView!.visibleCells() as! [CollectionViewcell] {
let indexpath : NSIndexPath = self.sectionImageCell._collectionView!.indexPathForCell(item as CollectionViewcell)!
let cell : CollectionViewcell = self.sectionImageCell._collectionView!.cellForItemAtIndexPath(indexpath) as! CollectionViewcell
//Close Button
if touchedIndexPath == indexpath {
if cell._closeBtn.hidden == false {
cell._closeBtn.hidden = true
}
else {
cell._closeBtn.hidden = false
}
}
}
}
}
}