這是一個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)
}
當我刪除該項目時,它可以刪除並與動畫漸變
如果'delegation.deleteCell(路徑,個體經營)'是在你的視圖控制器中調用,什麼是'path',爲什麼你傳遞'self'作爲第二個參數。它應該是你選擇的'MyCollectionViewCell'嗎? – Lawliet
@Lawliet嗨! 'path'是一個'var path:IndexPath!','self'是因爲'delegation.deleteCell(path,self)'在MyCollectionViewCell類中使用,所以我認爲自己可以使用..... – Zoe
實際上他不需要路徑(路徑,自我)。他應該只使用(self),這樣他總是可以得到正確的indexPath,無論使用indexPathFor(cell:cell)發生刪除還是插入 –