How do I know that the UICollectionView has been loaded completely?我正试图在Swift中重现这个解决方案,但是我在阅读Obj-C时遇到了麻烦。有人可以帮忙吗?
答案 0 :(得分: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")
}
答案 1 :(得分:10)
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")
}
}
答案 2 :(得分:2)
迟到回答,
在viewDidAppear,您可以通过以下方式获取:
float height = self.myCollectionView.collectionViewLayout.collectionViewContentSize.height;
也许当你重新加载数据然后需要用新数据计算一个新的高度然后你可以通过以下方式得到它:当你的CollectionView在viewdidload上重新加载数据时添加观察者来监听:
[self.myCollectionView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionOld context:NULL];
然后添加波纹管功能以获得新高度或在collectionview完成重新加载后执行任何操作:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//Whatever you do here when the reloadData finished
float newHeight = self.myCollectionView.collectionViewLayout.collectionViewContentSize.height;
}
不要忘记删除观察者:
在viewWillDisappear
[self.myCollectionView removeObserver:self forKeyPath:@"contentSize" context:NULL];
有关详细信息,请查看答案https://stackoverflow.com/a/28378625/6325474
答案 3 :(得分:1)
更简单的方法是只使用collectionView的 performBatchUpdates 函数,如下所示:
collectionView?.performBatchUpdates(nil, completion: { _ in
// collectionView has finished reloading here
})
答案 4 :(得分:0)
在我的情况下,问题是集合视图没有完全重新加载,所以我们必须等到集合视图&#39; reloadData&#39;饰面:
self.customRecipesCV.reloadData()
DispatchQueue.main.async {
if let _index = self.selectCustomRecipeAtIndex {
let nextIndexpath = IndexPath(row:index, section:0)
self.customRecipesCV.scrollToItem(at: nextIndexpath, at: .centeredHorizontally, animated: true)
}
}
答案 5 :(得分:0)
这是我在使用此扩展名重新加载集合视图数据时加载指示器的实现。
extension UICollectionView {
func reloadData(completion: @escaping ()->()) {
UIView.animate(withDuration: 0, animations: { self.reloadData() })
{ _ in completion() }
}
}
... somewhere in code ...
collectionView.isHidden = true
loadingView.startAnimating()
DispatchQueue.main.async {
self.collectionView.contentOffset.x = 0
self.collectionView.reloadData {
self.loadingView.stopAnimating()
self.collectionView.isHidden = false
}
}
答案 6 :(得分:0)
为我工作,没有任何复杂性。
self.collectionView.reloadData()
self.collectionView.performBatchUpdates(nil, completion: {
(result) in
// ready
})
完成处理程序块,当所有操作完成时执行。如果所有相关动画成功完成,则此块采用单个布尔参数,该参数包含值true;如果所有相关动画被成功中断,则返回false。此参数可能为nil。