这是一个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)发生删除还是插入 –