协议功能在swift中不工作3
这是一个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)
}
当我删除该项目时,它可以删除并与动画渐变
的animationDidStop(_:finished:)
是没有得到所谓的,因为你指定的第一个参数是可选的,但事实并非如此。
顺便说一句,如果你指定MyCollectionViewCell
符合CAAnimationDelegate
(例如,在扩展,像下图),编译器会警告你这个问题:
它应该是:
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() { ... }
}
非常感谢你!其实我真的很糟糕......但是这是我的问题,我将在未来学习如何使用block!答案是非常节省我的时间,它工作得很好!但是Xcode向我展示了这个'将未呈现的视图快照成空快照。确保您的视图至少在屏幕更新后的快照或快照之前呈现一次。我正在寻找解决方法!再次感谢你! – Zoe
@Zoe你看到[这里](https://stackoverflow.com/questions/25884801/ios-8-snapshotting-a-view-that-has-not-been-rendered-results-in-an-empty- snapsho)? – Honey
@霍尼嗨! ,感谢您的链接,它看起来使用相机或UIImagePickerController ?!但我没有使用它们中的任何一个。需要找到其他解决方案,无论如何,谢谢! – Zoe
如果'delegation.deleteCell(路径,个体经营)'是在你的视图控制器中调用,什么是'path',为什么你传递'self'作为第二个参数。它应该是你选择的'MyCollectionViewCell'吗? – Lawliet
@Lawliet嗨! 'path'是一个'var path:IndexPath!','self'是因为'delegation.deleteCell(path,self)'在MyCollectionViewCell类中使用,所以我认为自己可以使用..... – Zoe
实际上他不需要路径(路径,自我)。他应该只使用(self),这样他总是可以得到正确的indexPath,无论使用indexPathFor(cell:cell)发生删除还是插入 –