UICollectionView什么时候完成加载?

Gra*_*ark 14 swift

我如何知道UICollectionView已完全加载?我试图在Swift中重现这个解决方案,但是我在阅读Obj-C时遇到了麻烦.有人可以帮忙吗?

Sal*_*ior 11

SWIFT 3: @ libec的答案版本

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.old, context: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    collectionView.removeObserver(self, forKeyPath: "contentSize")
}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let observedObject = object as? UICollectionView, observedObject == collectionView {
        print("done loading collectionView")
    }
}
Run Code Online (Sandbox Code Playgroud)


lib*_*bec 10

如果你想从你所链接的问题中采用这种方法,那么:

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView?.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.Old, context: nil)
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if let observedObject = object as? UICollectionView where observedObject == self.collectionView {
        print(change)
    }
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    self.collectionView?.removeObserver(self, forKeyPath: "contentSize")
}
Run Code Online (Sandbox Code Playgroud)