2017-07-09 32 views
2

這是一個cardCollectionView應用程序,我試圖用longpress刪除項目並在項目中顯示一個刪除按鈕。我可以刪除項目,但我的項目不會重新加載到空的地方。我發現問題是因爲協議功能無法工作。協議功能在swift中不工作3

這裏是我的協議:

@objc protocol ActionDelegation:class { 

func deleteCell(_ indexPath:IndexPath, _ cellView:MyCollectionViewCell) 
func hideAllDeleteBtn() 
func showAllDeleteBtn()  
} 

hideAllDeleteBtn()showAllDeleteBtn()功能運作良好,但deleteCell(_ indexPath:IndexPath, _ cellView:MyCollectionViewCell)功能永遠不會奏效。

weak var delegation : ActionDelegation! 

我嘗試print()這裏,但在此功能在所有從未運行(在MyCollectionViewCell類)

func animationDidStop(_ theAnimation: CAAnimation!, finished flag: Bool){ 
    delegation.deleteCell(path, self) 
} 

這裏是視圖控制器類

我在功能

的人做 cell.delegation = self

它應該工作後,我點擊刪除按鈕與disspare動畫,

func deleteCell(_ indexPath:IndexPath, _ cellView:MyCollectionViewCell){   print("1") 
    myCollectionView.performBatchUpdates({() -> Void in 
     print("2") 
     self.cellArray.removeObject(at: indexPath.row) 
     self.myCollectionView.deleteItems(at: [indexPath]) 
     print("3") 
    }, completion: {(flag:Bool) in 
     print("4") 
     self.myCollectionView.reloadData() 
     print("5") 
    }) 
} 

yup ...這個函數從來沒有工作,如果這是函數工作,那麼空的地方不應該是空的。 僅供參考其他兩個協議功能正在工作,爲什麼只有這一個不能?

編輯

這是動畫的一部分,它是在一個延伸的NSObject

class Animation: NSObject { 

    func fadeAnimation(view:UIView){ 
    let animation = CATransition() 
    animation.delegate = view as? CAAnimationDelegate 
    animation.duration = 0.5 
    view.layer.add(animation, forKey: nil) 
    view.isHidden = true 
}} 

一個Animation類它將在MyCollectionViewCell 調用等的下方(MyCollectionViewCell

let animation = Animation() 

func setAnimation(){ 

    animation.fadeAnimation(view: self) 
} 

當我刪除該項目時,它可以刪除並與動畫漸變

when I delete it

+0

如果'delegation.deleteCell(路徑,個體經營)'是在你的視圖控制器中調用,什麼是'path',爲什麼你傳遞'self'作爲第二個參數。它應該是你選擇的'MyCollectionViewCell'嗎? – Lawliet

+0

@Lawliet嗨! 'path'是一個'var path:IndexPath!','self'是因爲'delegation.deleteCell(path,self)'在MyCollectionViewCell類中使用,所以我認爲自己可以使用..... – Zoe

+1

實際上他不需要路徑(路徑,自我)。他應該只使用(self),這樣他總是可以得到正確的indexPath,無論使用indexPathFor(cell:cell)發生刪除還是插入 –

回答

2

animationDidStop(_:finished:)是沒有得到所謂的,因爲你指定的第一個參數是可選的,但事實並非如此。

順便說一句,如果你指定MyCollectionViewCell符合CAAnimationDelegate(例如,在擴展,像下圖),編譯器會警告你這個問題:

error message

它應該是:

extension MyCollectionViewCell: CAAnimationDelegate { 
    func animationDidStop(_ anim: CAAnimation, finished flag: Bool) { 
     ... 
    } 
} 

未來,我可能會建議使用基於塊的動畫來簡化與動畫有關的任何與委託相關的問題。

protocol ActionDelegate: class { 
    func deleteCell(_ cell: UICollectionViewCell) 
    func hideAllDeleteBtn() 
    func showAllDeleteBtn() 
} 

class MyCollectionViewCell: UICollectionViewCell { 
    weak var delegate: ActionDelegate? 

    func fadeAndDelete() { 
     UIView.animate(withDuration: 0.5, animations: { 
      self.alpha = 0 
     }, completion: { _ in 
      self.delegate?.deleteCell(self) 
     }) 
    } 
} 

注意,我簡化deleteCell(_:)委託方法,因爲你不應該保存IndexPath,而是計算它只是在時間:

extension ViewController: ActionDelegate { 
    func deleteCell(_ cell: UICollectionViewCell) { 
     guard let indexPath = myCollectionView.indexPath(for: cell) else { return } 

     cellArray.removeObject(at: indexPath.row) // I might suggest making `cellArray` a Swift array rather than a `NSMutableArray` 
     myCollectionView.deleteItems(at: [indexPath]) 
    } 

    func hideAllDeleteBtn() { ... } 

    func showAllDeleteBtn() { ... } 
} 
+0

非常感謝你!其實我真的很糟糕......但是這是我的問題,我將在未來學習如何使用block!答案是非常節省我的時間,它工作得很好!但是Xcode向我展示了這個'將未呈現的視圖快照成空快照。確保您的視圖至少在屏幕更新後的快照或快照之前呈現一次。我正在尋找解決方法!再次感謝你! – Zoe

+0

@Zoe你看到[這裏](https://stackoverflow.com/questions/25884801/ios-8-snapshotting-a-view-that-has-not-been-rendered-results-in-an-empty- snapsho)? – Honey

+0

@霍尼嗨! ,感謝您的鏈接,它看起來使用相機或UIImagePickerController ?!但我沒有使用它們中的任何一個。需要找到其他解決方案,無論如何,謝謝! – Zoe